• Resolved seocoalition

    (@seocoalition)


    Thanks for reading this post and helping me out!

    In my template I am using wp_list_categories to list my categories in my blog. Well I am using Article Directory plugin and I have tons of different categories for my directory.

    I am using:
    <?php wp_list_categories('title_li=&hierarchical=0&show_count=1&number=5') ?>

    I would like to create a random categories list instead of using the first 5 like I am now.

    I am no PHP guru and would like some help to create the code.

    TIA (Thanks In Advance)
    Rex

Viewing 6 replies - 1 through 6 (of 6 total)
  • Might be a better way, but this should work:

    <?php
    // display 5 random categories
    $cats ='';
    $categories=get_categories();
    $rand_keys = array_rand($categories, 5);
    foreach ($rand_keys as $key) {
    $cats .= $categories[$key]->term_id .',';
    }
    wp_list_categories('title_li=5 Random Categories&hierarchical=0&hide_empty=0&show_count=1&include='.$cats);
    ?>

    Thread Starter seocoalition

    (@seocoalition)

    Michael H:

    I am getting this error my friend:

    Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array in /home/*******/public_html/**********/wp-content/themes/********/footer.php on line 20
    
    Warning: Invalid argument supplied for foreach() in /home/********/public_html/**********/wp-content/themes/********/footer.php on line 21

    Anything I can do to fix it?

    Thanks,
    Rex

    How many categories do you have? Need 5 or more.

    Thanks @michaelh
    Works perfectly fine for me.

    Thread Starter seocoalition

    (@seocoalition)

    MichealH:

    I must have put down the code wrong it is working now!

    Your the man with the plan…

    Thanks,
    Rex

    How would I be able include or exclude specific categories in that above code?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Random Categories for wp_list_categories’ is closed to new replies.