Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

Making wordpress recognise the home link as the current-menu-parent

Blog articles sit under the home page

         

johnnie

1:19 pm on Nov 12, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

On my new website, I have a two level menu that depends on the current-menu-parent and current-menu-item classes to display the correct submenu.

This all works fine for pages and my custom taxonomy, but in order to display the right submenu for blog posts, I need wordpress to apply the 'current-menu-parent' class to the homepage link in the menu when a user opens a blog article. My website is already set to 'display the latest posts' on the homepage under the reading settings.

Note that my permalink structure is such that all posts sit under a '/blog/' slug, but removing this doesn't do the trick.

It's rather hard to come up with a generic example, so I'm happy to PM the URL if anybody doesn't understand what I mean.

Any ideas?

not2easy

4:18 pm on Nov 12, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Was your menu created using the theme's Appearance > Menus panel?

Are you using the WP coding:
<?php wp_nav_menu( $args ); ?>

to add settings? They have a reference in the Codex for usage if this is what you are using: [codex.wordpress.org...]

johnnie

9:07 pm on Nov 12, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes to both. Will investigate.

johnnie

6:52 am on Nov 13, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraid the wp_nav_menu does not offer any solutions to my problem: how do I make wordpress recognise '/' as the parent of my blog posts? I basically want the 'home/blog' menu item to be flagged as active when users are browsing teh blog.

lorax

11:12 am on Nov 13, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Would testing the current URL and using an if/else statement work?

johnnie

11:09 pm on Nov 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lorax,

That could work, but how do I go about if/else-ing the items of a wordpress menu?

Or should I just solve this client side with some jquery and attach the class? I could easily match the string 'blog' in the current URL and assign the active class to the home page link.

Obviously this is not ideal since it breaks the site for people who have JS disabled, but it works.

lorax

12:14 pm on Nov 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would look into wp_nav_manu() [codex.wordpress.org...]

Forum discussion on this: [wordpress.org...]

TechBuddy

2:25 pm on Nov 18, 2014 (gmt 0)



write this code in function.php
<?php
register_nav_menus( array(
'Mainmenu' => __( 'Main Menu', 'xyz' ),
'Footermenu' => __( 'Footer Menu', 'xyz' ),
) );


and put this code where you show your menu and go to admin pannel -> Apperience -> customize -> set current page as static page, and set the menu from Appreince -> menu , accorind your menu list and drage home page in to the menu structure .

<?php wp_nav_menu( array( 'container_class' => '', 'menu_class' => 'menu', 'theme_location' => 'Mainmenu' ) ); ?>

johnnie

10:54 pm on Nov 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As said before, I already make use of the WP menu functionality.

How will this make wordpress recognise the home page as the parent for the blog posts? All page hierarchies etc. are recognised by the menu, but blog posts are seen as 'parentless' in my menu, since none of the items get assigned the 'current-page-parent' class.

Basically, what I want is for wordpress to recognise the 'home' link in the menu as the parent of the blog posts.