creating sub category navigation
-
For those people who want subcategories on their single.php pages, this is how I solved it. I just wanted to share it since it seems every plugin doesn’t work at the moment with the latest version of wordpress. First thing I did was make the required categories, ‘fruit’ and ‘vegtables’. You have to do this first since wordpress wants the category slugs to be unique but it is okay to name your menus after the category slugs later on. So after that I made 3 menus. The ‘main’ menu which contain links to the ‘fruit’ and ‘vegtables’ categories. Then made the menus for ‘fruit’ and ‘vegtables’.
On the homepage I use the following code as my main navigation:
<?php wp_nav_menu( array('menu' => 'main' )); ?>For the category page I use:
<?php $cat_object = $wp_query->get_queried_object(); $parentcat = ($cat_object->category_parent) ? $cat_object->category_parent : $cat; echo "<ul>"; wp_list_cats("child_of=$parentcat"); echo "</ul>"; ?>And on the single.php website I use:
<?php if(strstr($_SERVER['HTTP_REFERER'],'fruit')) { echo "<ul>"; wp_nav_menu( array('menu' => 'fruit' )); echo "</ul>"; } elseif (strstr($_SERVER['HTTP_REFERER'],'vegtables')) { echo "<ul>"; wp_nav_menu( array('menu' => 'vegtables' )); echo "</ul>"; } else { echo "<ul>"; wp_nav_menu( array('menu' => 'main' )); echo "</ul>"; } ?>The last thing I did to get it working the way I wanted was change the ‘Permalink Settings’ to ‘Custome settings’:
/%category%/%postname%/The reason I do it this way is because wordpress single.php doesn’t have any options to grab the category information from the page it just came from and didn’t want to create category and single templates because of a navigation. Hope I made some folks happy with it. If you guys have any other tips to get something similar set up then I’d be keen to see your approach.
The topic ‘creating sub category navigation’ is closed to new replies.