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.
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.
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!
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With