Site logo

Archived topic

Adding new menu to CPT only. Are GP hooks the same in CPT? Found Visual Hooks Gu

3 replies · Started by Brad on August 16, 2019

Viewing posts 1–4 of 4

Trying to conditionally add a menu just to CPT individual posts (not the CPT archive page though my conditional isn't working on the archive page either). Not sure if this is possible or if my code is just incorrect. Possibly if statement is incorrect? Do I have the right GP hook? Any ideas?

Gist line 22 - 24

Thanks in advance for taking a look.

https://codex.wordpress.org/Function_Reference/is_post_type_archive

https://codex.wordpress.org/Conditional_Tags

https://gist.github.com/bradfranklin/9efe2adc4c0ed622a0cee3fb0d7aad06

Hi there,

Yes, as long as you're not using a custom template file with the hooks removed.

Your code itself isn't quite right. Try this:

add_action( 'generate_after_header', function() {
    if ( is_post_type_archive( 'ambassadors' ) ) {
        wp_nav_menu( array(
            'menu' => 'CPT Ambassador Menu', // Do not fall back to first non-empty menu.  Was Primary Menu
            'theme_location' => 'cpt-ambassador-menu',
            'fallback_cb' => '__return_false'             // Do not fall back to wp_page_menu()
        ) );
    }
} );

Our Elements module might be worth checking out as well, as it writes some of the above for you: https://docs.generatepress.com/article/hooks-element-overview/

Sincerely, sincerely appreciate the help!! Going nuts trying different variations.

Switched out post_type_archive for is_singular to achieve objective.

if ( is_singular( 'ambassadors' ) ) {

Alternately was thinking about duplicating the top bar widget area, placing it with an if statement in the same area or below the menu bar instead of above, then just using the nav widget and choosing the menu of choice. All the css is taken care of with the nav widget

A custom menu is likely the way to go. Glad you got it working :)

This archived topic is closed to new replies.