1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to Display Random Categories on a Template page?

Discussion in 'WordPress' started by misohoni, Feb 6, 2015.

  1. #1
    I've done some searching and I can only pick up:
    "display random posts from a category", but I'd like to be able to offer random categories..

    How could I do this? Thanks
    SEMrush
     
    misohoni, Feb 6, 2015 IP
    SEMrush
  2. dedeblank

    dedeblank Active Member

    Messages:
    135
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    The logic is Put the cat id into array. And random it. Then put the result to wp query.
     
    dedeblank, Feb 6, 2015 IP
  3. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #3
    ok cool, how could I do that? I couldn't see any examples on the web...
     
    misohoni, Feb 6, 2015 IP
  4. dedeblank

    dedeblank Active Member

    Messages:
    135
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    here is the sample. not tested yet
    $category_ids = array_rand(get_all_category_ids()); //get all category
    $args = array(
    'orderby' => 'rand',
    'cat' => $category_ids,
    'posts_per_page' => '1',
    );

    $query = new WP_Query( $args);//wp query

    //loop the query result
    if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    } else {
    // no posts found
    }

    wp_reset_postdata();
     
    dedeblank, Feb 6, 2015 IP
    misohoni likes this.
  5. joshuthomas

    joshuthomas Well-Known Member

    Messages:
    595
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    140
    #5
    its very easy - search for a plugin called "SRP" or "special recent posts". you can configure what to show on the page from which all category or random using a short code.
     
    joshuthomas, Feb 11, 2015 IP