最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - current-post-parent for custom post type

programmeradmin1浏览0评论

I have 2 archive pages on my site. First "News" is for standard wordpress posts. Second "Projects" is for my custom post type. When I go to specific post from "News" archive then my menu position "News" should be active. But it doesn't. So I do small refactor in my nav walker (I'm using Bulmascores_Nav_Walker by Seyong Cho). So I made something like this

     if (in_array('current-menu-item', $classes) || in_array('current-post-parent', $classes)  ) {
        $class_names .= 'is-active';
    }

And it working like it should. Problems starts when I want to do the same for my custom post type. I made print_r for classes and I can't see 'current-post-parent'. Below full code for this class

    public function getLinkButton($item)
{
    $url         = $item->url ?? '';
    $classes     = empty($item->classes) ? array() : (array) $item->classes;
    print_r($classes);
    $class_names = '';

    if (in_array('current-menu-item', $classes) || in_array('current-post-parent', $classes)  ) {
        $class_names .= 'is-active';
    }

    $button = sprintf("<a href='%s' class='navbar-item %s'>%s</a>", $url, $class_names, $item->title);

    return $button;
}

Array for active custom post type item

Array
(
    [0] => 
    [1] => menu-item
    [2] => menu-item-type-post_type_archive
    [3] => menu-item-object-projekty
)

it should contain current-post-parent but I don't know how to achieve this. So, anyone could help me, please?

发布评论

评论列表(0)

  1. 暂无评论