|

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.  

  • | |

    Directory Handler at Theme Forest

    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!I’m happy to announce my first Theme Forest component, Directory Handler. it’s a simple set of 2 classes that handle (in…

  • |

    Regarding Building WordPress Themes from Scratch

    Lately I have been receiving a lot of questions regarding the availability of my book, Building WordPress Themes from Scratch. After speaking with the publisher, Rockable Press, it seems they’ve had to make some unfortunate changes regarding their distribution model. From their website: All Rockable Press books are now exclusively available with any Tuts+ Premium subscription,…

  • |

    How to Learn a New Programming Language

    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! As I said in the last two posts, Google I/O was truly inspiring. It got me to thinking about how…

  • |

    Excerpt from Building WordPress Themes from Scratch

    Recently my first book, Building WordPress Themes from Scratch, made its way back onto the market. In this book, I teach you how to use WordPress as well as how to leverage the API to create your own custom themes, plugins, and content types. Here is an excerpt from the book. Enjoy!

  • |

    Building Web Apps with WordPress

    I just finished reading up a book that was co-written by my friend Jason Coleman called Building Web Apps with WordPress. I picked it up because while I’m generally good at developing themes, I know there were some things I was missing- types of code, optimizations, plugin best practices, etc. I wanted to improve my WordPress…

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.