WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to add custom post type archive slug to nav menu? (7 posts)

  1. Harryson
    Member
    Posted 2 years ago #

    I cannot figure this out. There seems to be no checkbox or anything similar in function to add the newly created custom post type archive page into the menu.

    Also the link must be active, if you are on the page. This probably isn't possible, but is there some plugin for it? For the love of me, I cannot find it :(

    It is surprising that such important function isn't highlighted or easy to find. As most of stuff will be custom post type. In my case 90% of my site will use custom post types, and if I'm missing 90% of my links or they won't work like they should.. then that's a very huge problem.

  2. Harryson
    Member
    Posted 2 years ago #

    Ok found the plugin Custom Post Type Archives in Nav Menus, however it just adds a custom link and doesn't really help anything..

  3. Harryson
    Member
    Posted 2 years ago #

    Up, still cant figure this out.

  4. Harryson
    Member
    Posted 2 years ago #

    Maybe it is possible by overriding everything and adding the menu links to the nav manually? And also detecting the active link with $_GET.

  5. Harryson
    Member
    Posted 2 years ago #

    Based on the answer I got from: http://wordpress.stackexchange.com/questions/35593/how-to-add-custom-post-type-archive-page-links-to-nav-menu

    I forked that function to be more dynamic:

    add_filter('wp_list_pages', 'new_nav_menu_items');
    add_filter('wp_nav_menu_items', 'new_nav_menu_items');
    function new_nav_menu_items($items) {
    	global $wp_query;
    	$class = 'menu-item menu-item-type-post_type menu-item-object-page';
    	foreach ($GLOBALS['posttypelinks'] as $link) {
    		$items .= '<li class="' . $classes . ' ' . (isset($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] == $link['post_type'] ? 'current_page_item' : '') . '"><a href="' . site_url() . '/' . $link['slug'] . '">' . $link['title'] . '</a></li>';
    	}
    	return $items;
    }

    So, I could add my post-types to menu like this (inside create_post_type()):

    $GLOBALS['posttypelinks'][] = array(
    	'title' => $title,
    	'post_type' => $post_type,
    	'slug' => $slug
    );

    These variables are the same, that are being used inside register_post_type()'s array. Probably could be better solution and maybe in the form of a plugin..however, it does the trick perfectly.

  6. artsfantasy
    Member
    Posted 2 years ago #

    Ok

    If you want to add the whole Custom post into the menu ( archive ) there are 2 ways:

    1. The clean way
      Custom Post types have their own permalink - yoursite.com/taxonomy/custompost. So in the menu navigation you create your Custom Menu Item with a link to yoursite.com/taxonomy/

      After thats done you can customize the taxonomy with a template that sould be named as follows:
      taxonomy-your-custom-post-type-name.php
      and then you code it as you wish.

    2. the developer friendly way
      create a custom Page template and force the loop of custom posts like so:

      <?php
      $loop = new WP_Query( array( 'post_type' => 'POST TYPE', 'posts_per_page' => 10 ) );
      while ( $loop->have_posts() ) : $loop->the_post();
      ?>
      
      your loop stuff
      
      <?php endwhile; ?>

    If you want to add a specific custom post ( or a category ) then ytou should be able to do that in WP nav menu section. MAybe check the screen options.

    If its not there, then your code fr the custom posts is not set to display it in the menu.

  7. artsfantasy
    Member
    Posted 2 years ago #

    oh.... forgot to add that in point 2 - after creating a custom page template:
    create a regular page and save it with the new page template that you have just created.
    then ofc add that to the menu.

Topic Closed

This topic has been closed to new replies.

About this Topic