Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom post type archive to menu

Tags:

I have been searching for weeks and I still haven't found a proper solution to this problem.

I am writing a Wordpress Theme. I have a custom post type called Works. I would like to add my Works archive to my menu and have it as well as it's posts highlighted when I access them.

I can access my archive and posts on the following links

Works archive: /works/

Works single post: /works/postname/

My solution so fare have been to name my archive-works.php template file with a template name (Work archive). Then create an empty page using that template and adding the page to the menu. This highlights the archive in the menu but not the single posts.

I can easily solve this with a custom link and some javascript but there must be a better and cleaner way.

like image 567
rasmussvanejensen Avatar asked Jan 02 '14 08:01

rasmussvanejensen


People also ask

How do I add an archive to my menu?

Just go to Appearance, select Menus, and then click on Custom Link to see details. Enter the URL of the custom post type archive page in the URL field, add the label you want in the field Link Text, and click on Add to Menu and the custom link will appear in the right column. Remember to click on the Save Menu button.

How do I add posts to menu in WordPress?

If so, open up My Site > Customize > Menus. Use the Add Items button to add the post you want, then drag it up and down the menu to put it in the spot you'd like. For a submenu, drag it a little bit to the right, so it's indented. Then Save & Publish when it looks right in the preview!

How do I display custom post type?

Displaying Custom Post Types Using Default Archive Template First, you can simply go to Appearance » Menus and add a custom link to your menu. This custom link is the link to your custom post type.

How do I create a custom archive page for custom post type in WordPress?

Enable Archive for Your Custom Post Type in WordPress For instance, in CPT UI plugin, you can edit your custom post type and turn on the 'Has Archive' functionality under post type settings. On the other hand, if you used code to generate your custom post type, then you'll need to edit that code to enable archives.


1 Answers

You can do a simple trick in your functions.php:

add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2); function current_type_nav_class($classes, $item) {     // Get post_type for this post     $post_type = get_query_var('post_type');      // Go to Menus and add a menu class named: {custom-post-type}-menu-item     // This adds a 'current_page_parent' class to the parent menu item     if( in_array( $post_type.'-menu-item', $classes ) )         array_push($classes, 'current_page_parent');      return $classes; } 

In your case, you just have to add a class 'works-menu-item' with that archive menu item by the admin panel;

like image 94
Reza Mamun Avatar answered Sep 19 '22 14:09

Reza Mamun