Forums

How To Display Posts from random categories..? (2 posts)

  1. Raj
    Member
    Posted 6 months ago #

    I would like to Display Posts from random categories, I posts (it can be random or latest) from random category's should be displayed on the homepage.. is there any way to achieve this..?

    Regards
    Raj [sig moderated as per the Forum Rules]

  2. alchymyth
    The Sweeper
    Posted 6 months ago #

    get the site's categories, select one of them randomly, and make a custom query based on that catgory;

    example:

    <?php $cats = get_categories('hide_empty=1');
      $random = rand(0,count($cats)-1);
      $random_cat_id = $cats[$random]->term_id;
    $random_cat_posts = new WP_Query(
      array('category__in' => array($random_cat_id), 'posts_per_page' => 5)
      ); //use more query parameter if you need//
    if( $random_cat_posts->have_posts() ) : while( $random_cat_posts->have_posts() ) : $random_cat_posts->the_post();
      //OUTPUT WHATEVER YOU QANT TO SHOW PER POST//
    endwhile; endif; ?>

    http://codex.wordpress.org/Function_Reference/get_categories
    http://codex.wordpress.org/Class_Reference/WP_Query

Reply

You must log in to post.

About this Topic