Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Dynamic Categories in 3.0 Navigation WordPress

  • SOLVED

I have a completed theme built upon thematic framework. I am using the WP 3.0 menu functionality and can manually add Categories to the list. However, client wants this to be dynamic so that all the categories and sub-categories show up automatically without having to make changes to the Menu.

Here is my code in the functions.php that simply throws out the menu -


function childtheme_menu() { ?>
<div id="access">
<div class="skip-link"><a href="#content" title="<?php _e('Skip navigation to the content', 'thematic'); ?>">
<?php _e('Skip to content', 'thematic'); ?></a></div>
<?php wp_nav_menu( 'sort_column=menu_order&container_class=menu&menu_class=sf-menu&depth=3' ); ?>
</div>
<!-- #access -->
<?php }
add_action('thematic_belowheader','childtheme_menu',9);

Answers (3)

2010-09-01

enodekciw answers:

try this:
function childtheme_menu() { ?>
<div id="access">
<div class="skip-link"><a href="#content" title="<?php _e('Skip navigation to the content', 'thematic'); ?>">
<?php _e('Skip to content', 'thematic'); ?></a></div>
<?php wp_nav_menu( 'sort_column=menu_order&container_class=menu&menu_class=sf-menu&depth=3' ); ?>
<?php wp_list_categories('title_li='); ?>
</div>
<!-- #access -->
<?php }
add_action('thematic_belowheader','childtheme_menu',9);


Chris comments:

I've already tried this but it doesn't work because the wp_nav_menu encapsulates the menu in the proper div tags - this places it outside of that. What I need is the dynamic categories to be shown INSIDE the output produced by wp_nav_menu


enodekciw comments:

ok, so it wraps the menu. let's unwrap it ;)

function childtheme_menu() { ?>
<div id="access">
<div class="skip-link"><a href="#content" title="<?php _e('Skip navigation to the content', 'thematic'); ?>">
<?php _e('Skip to content', 'thematic'); ?></a></div>
<?php wp_nav_menu( 'sort_column=menu_order&container=&container_class=&menu_class=sf-menu&depth=3' ); ?>
<?php wp_list_categories('title_li='); ?>
</div>
<!-- #access -->
<?php }
add_action('thematic_belowheader','childtheme_menu',9);


enodekciw comments:

now it should generate plaind list items without the <ul></ul>. so just those two functions into <ul id="menu"></ul> or something and you should be good to go ;)


enodekciw comments:

oh, now I get your problem. you can try to solve it like this:

pass echo=false into wp_nav_menu().
use some variable to get the markup, something like this $menu = wp_nav_menu();
now you have $menu var which hold your menu markup, so now you can write some functions to manipulate it like you want, for example, you can write a function to extract list items, or just to get rid of the tags you dont need.
combine it with wp_list_categories();, wrap everything into <ul></ul> - now you have your menu working ;)

I would like to help and write the full code for you, but I'm leaving so soon that I won't be able to do that. But I will be happy if I got you on the way ;)


Chris comments:

enodekciw - you got me on my way. I had been playing around with this before i posted the question but wasn't able to get it working. Thanks to your help I was able to, and added all the nested ul / li mark-up which made it work just fine. Here is my final code:

function childtheme_menu() { ?>
<div id="access">
<div class="skip-link"><a href="#content" title="<?php _e('Skip navigation to the content', 'thematic'); ?>">
<?php _e('Skip to content', 'thematic'); ?></a></div>
<?php wp_nav_menu( 'sort_column=menu_order&container_class=menu&menu_class=sf-menu&depth=3' ); ?>
<div class="menu-cat">
<ul id="menu-category-nav" class="sf-menu sf-js-enabled">
<li class="menu-item" class="sf-menu"><a href="#">Categories</a>
<ul class="sub-menu"><?php wp_list_categories('title_li='); ?></ul>
</li>
</ul>
</div>
</div>
<!-- #access -->
<?php }
add_action('thematic_belowheader','childtheme_menu',9);

2010-09-01

Maor Barazany answers:

The simple and short answer is that you can't do dynamic menus (categories or pages) with the new wp_nav_menu functions.
You have to use either the nav function to let the end user manually decide what to have on the menu, or use the wp_list_categories function that was suggested here.


Chris comments:

Thanks for this Maor.

2010-09-01

Tobias Nyholm answers:

I have created a plugin for dynamic subpages: http://www.wordpress.org/extend/plugins/dynamic-subpages/

I think you can figure your problem out by watching my code.


Chris comments:

Tobias,

Looks like you are creating an entirely new menu system. Do you know how to make this function within the WP 3.0.1 menu system?