Support » Fixing WordPress » How to output the post tag within the WP_NAV_MENU

  • Ok so i have a custom post type of ‘projects’ set up and I would like to use the WP_NAV_MENU arguments to modify the output of my sub-menu. I want to display the “country” (set up as a tag) in front of the project name, but within the link.
    I currently have this:

    <?php 
    
    $tags = get_the_tags($post->ID);
    $country = '';
    foreach($tags as $tag) {
        $country = $tag->name;
    } 
    
    ?>
    wp_nav_menu( array(
        'before_link' => "<span class='country'>" . $country . " - </span><span>",
        'after_link' => '</span>',
    ) );

    I know this will add it onto all the nav items, but I added css to not display the span unless it’s within a submenu.

    But I’m not getting the correct result, and i know this is probably because it’s struggling to find the posts and thus can’t find any tags. But not entirely sure the best way to approach this. This result is instead I’m just getting an output of ‘1’ on all items.

    Ideal I’d like to output something like this:

    Tag Here – Project Name
    Tag Here – Project Name
    Tag Here – Project Name
    Tag Here – Project Name

    Any reccomendations?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘How to output the post tag within the WP_NAV_MENU’ is closed to new replies.