1

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?

0

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);
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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