Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have set my category navigation menu to display home and 5 of my categories and their respective posts when the category tab is clicked, as that is all the space there is to display in the menu bar.

I have more than 20 categories and I want the 5 category tabs to randomly display the categories on each browser refresh, so that all categories have a random chance to get displayed within the 5 category tabs.

The menu code in my header template is currently :-

<div id="nav_link"><!-- links -->
<ul class="nav"><!-- Navigation Links -->
        <?php if ( is_home() ) { ?>
          <li class="first"><a href="<?php echo get_option('home'); ?>" rel='nofollow'>Home</a></li>
        <?php } else { ?>
          <li><a href="<?php echo get_option('home'); ?>"rel='nofollow'>Home</a></li>
        <?php } ?>
<?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>
</ul><!-- End Navigation -->

which displays the standard category menu.

How can I achieve this?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I don't think you can use a menu for that, as menus are created manually in the Admin Panel.

You will have to use wp_list_categories like this:

$categories=get_categories();
$rand_keys = array_rand($categories, 5);
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
wp_list_categories('include='.$cats);
share|improve this answer

Thank you for the help, it worked & this is the code I used :-

<ul class="nav">
<?php if ( is_home() ) { ?>
<li class="first"><a href="<?php echo get_option('home'); ?>" rel='nofollow'>Home
</a></li>
<?php } else { ?>
<li><a href="<?php echo get_option('home'); ?>"rel='nofollow'>Home</a></li>
<?php } ?>
<?php
$categories=get_categories();
$rand_keys = array_rand($categories, 5);
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .','; 
}
wp_list_categories('title_li=&include='.$cats);
?>
</ul>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.