根据特定条件将自定义类添加到wordpress导航菜单项

时间:2013-07-19 10:44:05

标签: wordpress class menu conditional

如果是单页,我想将当前菜单项类添加到li。 我试图通过使用wp_nav_menu_objects钩子与自定义函数,但不知道如何获取特定的菜单项并设置条件将类分配给它。 这是代码。

add_filter('wp_nav_menu_objects' , 'my_menu_class');
function my_menu_class($menu) {

    //if it is a single post page of a particular post type (in this case 'property')
    if( is_single() && is_post_type_archive( 'property' ) ) {

    //get all the menu items
    foreach($menu as $key => $item) {

        // check if the menu item is "Commercial Property"
        if($item == "Commercial Property") {  

            //assign the class to that menu item
            $menu[$key]->classes[] = 'current-menu-item';
        }
    }
}   


return $menu;        
}

此代码仅用于表示逻辑。请建议使用此方法是否可以实现我需要的内容,或者有更好的方法。

感谢。

4 个答案:

答案 0 :(得分:1)

在我的WP主题中,我想删除所有wordpess菜单类,添加我自己的和特定条件(例如,如果菜单有一个活动类)添加另一个类。这是我的代码

function add_custom_classes($classes){

    $newClasses[] = "navigation__item";

    if( in_array('current_page_item', $classes) ){
        array_push($newClasses, "navigation__item-active");
    }

    return $newClasses;
}

add_filter('nav_menu_css_class', 'add_custom_classes', 100, 1);
add_filter('nav_menu_item_id', 'add_custom_classes', 100, 1);
add_filter('page_css_class', 'add_custom_classes', 100, 1);

您可以添加自己的条件

答案 1 :(得分:0)

is_single实际上正在寻找一个帖子。您需要is_page

答案 2 :(得分:0)

Wordpress已提供课程如果是单页

current-post-ancestor

在任何帖子详细信息页面上,您可以检查您将找到该课程的菜单,以便以其他方式使用current-post-ancestor课程来完成您需要做的事情

希望它有意义

答案 3 :(得分:0)

我已经使用HTTP_REFERER解决了它在同一个函数中设置条件。 不要觉得这是实现这一目标的最好方法,但它让我感到厌烦。

以下是其他人需要参考的代码。

function nav_menu_class($menu) {

    //To dynamically get the directory path from the URL irrespective of the local or web server.
$ref    = $_SERVER['HTTP_REFERER']; // prev page url.
$split_ref  = pathinfo($ref); // splitting the url to seperate the directory (http://dev.calyxagency.com/horizoncapital/site-new/) from the file path.
$dir_path  .= $split_ref[ "dirname" ]; // extracting the host + directory from the full path
$class      = 'current-menu-item'; // class to be assigned.

foreach($menu as $key => $item) {
    if( $menu[$key]-> ID == 101 ) { // check if the menu item matches this ID
        if( $ref == $dir_path.'/properties-to-let/' || $ref == $dir_path.'/properties-for-sale/' || $ref == $dir_path.'/commercial-property/' ) {
            $menu[$key]->classes[] = $class;
        }
    } elseif( $menu[$key]-> ID == 15 ) { // check if the menu item matches this ID
        if( $ref == $dir_path.'/business-for-sale/' ) { // check if the single page has come from this page for assigning current class to only this menu item 
            $menu[$key]->classes[] = $class;
        }
    } elseif( $menu[$key]-> ID == 18 ) { // check if the menu item matches this ID
        if( $ref == $dir_path.'/news/' ) { // check if the single page has come from this page for assigning current class to only this menu item 
            $menu[$key]->classes[] = $class;
        }
    }
} 
return $menu;   
}
add_filter('wp_nav_menu_objects' , 'nav_menu_class');
相关问题