Forums

Random posts from current cate (2 posts)

  1. maorb
    Member
    Posted 3 weeks ago #

    Hi,

    I'm looking for a widget that will be put on my archive/category/posts pages, that will show random posts from current category.
    All plugins I found only display random post from all site, or from chosen specific category.
    Is there a plugin to display random posts from the current category (when viewing a single post or category archive page) ?

    Thanks,

  2. MichaelH
    moderator
    Posted 3 weeks ago #

    If single post, for each category of that post, display 5 posts in that category, excluding the single post.

    Related:
    http://wordpress.org/search/category+single+sidebar?forums=1
    http://wordpress.org/support/topic/257858?replies=4
    http://wordpress.org/support/topic/179138?replies=4
    http://wordpress.org/support/topic/252228

    <?php
    if ( is_single() ) {
      $categories = get_the_category();
      if ($categories) {
        foreach ($categories as $category) {
          $cat = $category->cat_ID;
          $args=array(
            'cat' => $cat,
            'post__not_in' => array($post->ID),
            'showposts'=>5,
            'orderby'=>'rand',
            'caller_get_posts'=>1
          );
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
            echo '<h2>More Posts from '. $category->category_description . '</h2>';
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
             <?php
            endwhile;
          } //if ($my_query)
        } //foreach ($categories
      } //if ($categories)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>

    For your category archive, are you suggesting you just want random display of posts in that archive? If so you might try this before your loop:

    query_posts($query_string . '&orderby=rand');

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

Reply

You must log in to post.

About this Topic