|

Fix wp_nav_menu on Custom Type Archives in WordPress

Recently I was working with an issue in WordPress where the site’s menu was not showing up on an archive page for one of my custom post types. There were a few troubleshooting things I tried, including the most common recommendation, use theme_location instead of menu when referencing the menu in your theme (code after the jump):

wp_nav_menu( array(
    'theme_location' => 'main-menu-slug',
) );

However, that wasn’t working. I did find another solution that got me part of the way there, which removed the taxonomy specifications if WordPress was building the menu.

function fix_nav_menu( $query ) {
    if ( $query->get( 'post_type' ) === 'nav_menu_item' ) {
    $query->set( 'tax_query', '' );
    }
}
add_action( 'pre_get_posts', 'fix_nav_menu' );

This didn’t completely work for me because I was using a meta_key to sort my posts, so I changed the if statement to reset a couple more query variables:

if ( $query->get( 'post_type' ) === 'nav_menu_item' ) {
    $query->set( 'tax_query', '' );
    $query->set( 'meta_key', '' );
    $query->set( 'orderby', '' );
}

That did the trick! It took me quite a bit of troubleshooting, so hopefully this will help you out if you’re trying to fix wp_nav_menu on Custom Post Type Archives.

Similar Posts

  • |

    Out Now: Responsive Design with WordPress

    I’m happy to announce that my second book, Responsive Design with WordPress is out today online and in bookstores. It’s published through Peachpit/New Riders and I couldn’t be happier with the way it turned out.  

  • |

    Reuseability

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website! A few months ago I developed a Scrubs Quotes application for Facebook. This is a simple app that displays a…

  • |

    Join Me for CigarCamp at WordCamp US!

    With WordCamp US quickly approaching, I thought it would be fun to have an informal CigarCamp US while everyone is in town. Here are the details: Location: Ashton Cigar Bar (map) Date: Friday, December 4th Time: 9:00pm – 11:00pm EST Head on over to the site and sign up! There will be cigars, scotch, and WordPress talk. And…

  • | |

    Mobile Matters! Google to change Search Rankings

    If you’ve done a Google Search on your mobile device recently, you may have noticed that Google adds a bit of text in the search results alerting you to the fact that a website is (or is not) mobile friendly. This can have a big impact on your website’s search ranking.

  • |

    New Design- Dynamics

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!This is part 3 of 5 of the mini series for my new design. In this part I will be talking…

  • | |

    Announcing 1085 & a Beginner’s WordPress Workshop

    When the weekly coding sessions were announced, I also alluded to courses, or workshops, that will be taking place at the Launch Pad in Downtown Scranton. I’m very excited  to announce the first one, as well as the venture behind it. First, the Workshop: This is one that’s been requested to me personally a few times…

One Comment

  1. It seems that this breaks the menu’s on all pages and anywhere the wp_nav_menu is called on the site since WP6.0.

    There is a bug in 6.0 that when this is in the functions.php file the WP menu adds every menu item and other wp nav menus to each wp nav on both the backend in the menu editor and the frontend of the site. Makes for a very large menu anywhere a wp menu is called.

    Example: https://accelerated.local/

Comments are closed.