自定义帖子类型父项的当前菜单项类

时间:2013-11-04 作者:Richard Mišenčík

我的菜单和自定义帖子类型有问题。

我有一个自定义的“服务”帖子类型。我已经为它创建了一个名为“服务”的新页面。在该页面上,Im显示了来自该自定义帖子类型的所有帖子的列表。当前菜单项类正在按预期工作。

但问题是当我点击其中一个服务并转到mysite时。com/services/service-1,菜单中services页的当前菜单项消失。我需要表明,这篇当前的帖子是服务页面的子页面。

所有菜单项都具有相同的HTML:

<li id="menu-item-23" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-23"><a href="http://localhost/wordpress/sluzby/">Služby</a></li>
没有CSS类可用于将此链接样式设置为父链接。类似于当前菜单父项或其他内容。我怎样才能解决这个问题?谢谢

4 个回复
最合适的回答,由SO网友:Joey Yax 整理而成

我通常在插件中包含以下父变量、过滤器和方法来解释这种情况。我从来都不确定这是不是一种“正确”的方式,但感觉比用javascript应用要好。

class Plugin_Name {
    private $parent = \'services\'; // ideally this is a setting in your plugin, not a hard-coded variable in case the page slug changes

    function __construct() {
        // Add classes to \'parent\'
        add_filter( \'nav_menu_css_class\', array( &$this, \'nav_parent_class\' ), 10, 2 );
    }

    function nav_parent_class( $classes, $item ) {

        if ( $this->nicename == get_post_type() && ! is_admin() ) {
            global $wpdb;

            // remove any active classes from nav (blog is usually gets the currept_page_parent class on cpt single pages/posts)
            $classes = array_filter($classes, ($class == \'current_page_item\' || $class == \'current_page_parent\' || $class == \'current_page_ancestor\'  || $class == \'current-menu-item\' ? false : true ));

            // get page info
            // - we really just want the post_name so it cane be compared to the post type slug
            $page = get_page_by_title( $item->title, OBJECT, \'page\' );

            // check if slug matches post_name
            if( $page->post_name == $this->parent ) {
                $classes[] = \'current_page_parent\';
            }

        }

        return $classes;
    }
}
根据您的请求,非插件路由。我没有测试这是否有任何错误,只是根据上面的课程改编,但它至少应该让你走上正确的方向:

add_filter( \'nav_menu_css_class\', \'nav_parent_class\', 10, 2 );

function nav_parent_class( $classes, $item ) {
    $cpt_name = \'service\';
    $parent_slug = \'services\';

    if ( $cpt_name == get_post_type() && ! is_admin() ) {
        global $wpdb;

        // remove any active classes from nav (blog is usually gets the currept_page_parent class on cpt single pages/posts)
        $classes = array_filter($classes, ($class == \'current_page_item\' || $class == \'current_page_parent\' || $class == \'current_page_ancestor\'  || $class == \'current-menu-item\' ? false : true ));

        // get page info
        // - we really just want the post_name so it cane be compared to the post type slug
        $page = get_page_by_title( $item->title, OBJECT, \'page\' );

        // check if slug matches post_name
        if( $page->post_name == $parent_slug ) {
            $classes[] = \'current_page_parent\';
        }

    }

    return $classes;
}

SO网友:Marek

如果管理面板中没有嵌套的菜单结构,则此代码会将类“current menu item”添加到子CPT的父项菜单或自定义分类法或默认单篇文章中-仅当您有“level 0”菜单时。例如,如果您有显示产品网格的页面Product,并且您转到单个产品,则WP将看不到父菜单项。下面的代码改进了这一点:

function additional_active_item_classes( $classes = array(), $menu_item = false ) {
    // custom taxonomy
    if ( $menu_item->title == \'Custom Tax Name Page\' && is_tax(\'custom_tax\') ) {
        $classes[] = \'current-menu-item\';
    }
    // custom post type single
    if ( $menu_item->title == \'Custom Post Type Page\' && is_singular(\'products\') ) {
        $classes[] = \'current-menu-item\';
    }
    // blog post single
    if ( $menu_item->title == \'Blog Page\' && is_singular(\'post\') ) {
        $classes[] = \'current-menu-item\';
    }
    return $classes;
}
add_filter( \'nav_menu_css_class\', \'additional_active_item_classes\', 10, 2 );

SO网友:Richard Mišenčík

Final code:

function nav_parent_class($classes, $item) {
    $cpt_name = \'services\';
    $parent_slug = \'sluzby\';

    if ($cpt_name == get_post_type() && !is_admin()) {
        global $wpdb;

        // get page info (we really just want the post_name so it can be compared to the post type slug)
        $page = get_page_by_title($item->title, OBJECT, \'page\');

        // check if slug matches post_name
        if( $page->post_name == $parent_slug ) {
            $classes[] = \'current_page_parent\';
        }

    }

    return $classes;
}

add_filter(\'nav_menu_css_class\', \'nav_parent_class\', 10, 2);
SO网友:Joe

通常有一类。当前菜单祖先添加到导航中的父项。

如果没有,请查看此帖子:How to include the 'current-menu-ancestor' class on a custom post type menu in Wordpress?

结束

相关推荐

Menus like a CMS

以下是一些内容的结构示例:Parent A -- Child A -- Child B Parent B -- Child C -- Child D Parent A 应链接到的内容Child A 和Parent B 应链接到的内容Child C. 除此之外,我不确定这是否应该完全由wordpress中的“菜单”来管理,或者它是否应该是Parent A 包含子页面的页面。唯一的问题是Parent A 不是真正的内容,只是链接到Child A.如果你需要一个真