jovvla
Forum Replies Created
-
Forum: Themes and Templates
In reply to: [Openstrap] Front Page Template without Slider Not showing PageSame problem here…
Using theme version 1.0.9 and WP 4.1Forum: Plugins
In reply to: How to build Dependent SidemenuI have found a solution in case someone else is interested in it.
It actually involves relatively small amount of programming together with couple of different adjustments to the default setup.
1.) In case you’re using mostly static pages on your site, like I do, you’ll need to install plugin called “Post Tags and Categories for Pages”. It offers, as it’s name says, the possibility to add category to your pages which will be crucial for selecting them later, when needed. Posts, on the other hand, can be categorized natively, without mentioned plugin. Either way, each page and post you would like to have as a menu link should have a category assigned to it. Within wp-admin panel under Pages/Categories you can now Add New Category. Do so, by keeping in mind that all pages you assign that category to should be part of the same sidemenu.
2.) Then you’ll need to register sidemenus you would like to use by adding few lines of code to your functions.php file. Something like:// Register sidebar menu set function register_custom_menus() { register_nav_menus( array( 'menu-category01' => __( 'menu-category01', 'mytheme'), 'menu-category02' => __( 'menu-category02', 'mytheme'), 'menu-category03' => __( 'menu-category03', 'mytheme'), ) ); } add_action( 'init', 'register_custom_menus' );
Feel free, of course, to change menu names to your needs, especially because they will have to match the names of the categories you’ll assign to your pages and/or posts.
3.) Next, create all pages that will be part of your future sidemenus. Each page you create (Pages/Add New Page) should have appropriate category assigned (check it’s name in the All Categories list) to it and under Page Attributes it’s crucial to choose it’s Parent (that page’s ancestor from the main menu) and Template (Sidebar/Content, that is already in the list).
4.) After that, sidemenus need to be created under Appearance/Menus. Just click to create new menu and type in it’s name (that must match the category name of all pages that will be part of that menu). When adding pages to new sidemenu be sure that you have checked it’s Theme Location (the list in the bottom of the page with the names like menu-category01).
5.) You’re nearly there, but still there are a few lines of code to be added to the sidebar template called sidebar-left.php. Just above the line that says “above widgets hook” add the following:<?php $cat = get_the_category(); $cat = $cat[0]->cat_name; // echo('<p>Category: <strong>'.$cat.'</strong></p>'); // uncomment the line to check/visualise if the category name is returned correctly if( !empty( $cat ) ) { wp_nav_menu(array('theme_location' => 'menu-'.$cat, 'container_class' => 'sidemenu' , 'fallback_cb' => false)); } ?>
6.) Previous function will insert the appropriate sidemenu in the left sidebar area of your theme and will add CSS class ‘sidemenu’ that you can use to style it to your needs. Speaking about CSS, by default there is the class called ‘current-page-ancestor’ assigned to, well, each page’s ancestor and you can use it to keep the parent item in the main menu active/highlighted.
That’s all folks!
It took me quite a while to collect all these pieces together, but now I have the desired dependent menu structure in place and operational. I hope it could help someone trying to achieve the same menu logic using wordpress.
I’m sure there are other approaches in getting this right and if you could share them it will be great.
Take care.