最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - current-post-parent for custom post type

matteradmin7PV0评论

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?

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far