Forums

[resolved] More posts from same cat -current one? (4 posts)

  1. Amanda_a
    Member
    Posted 7 months ago #

    Thank u for ur time first and foremost!

    My wordpress posts are split into categories. When u arrive at a specific post in the sidebar I'm trying to display more posts from that same category.

    I am using:
    query_posts("showposts=3&orderby=rand&cat='.$category_id");

    Where $category_id is defined here:

    <?php
    
    foreach((get_the_category()) as $category) {
    $category_id = $category->cat_ID . '';
    $category_name = $category->cat_name . '';
    	}
    ?>

    The problem is that I notice the post I am viewing appearing in the randomly generated query on my sidebar. Is there a way to ensure the post I am on doesn't appear within this query?

    The beta can be seen here: Beta

  2. alchymyth
    The Sweeper
    Posted 7 months ago #

    to exclude the current post from the random list, change this:

    query_posts("showposts=3&orderby=rand&cat='.$category_id");

    to:

    global $post; query_posts( array('posts_per_page' => 3, 'orderby' => 'rand', 'cat' => $category_id, 'post__not_in' => array($post->ID)) );

    http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

  3. Amanda_a
    Member
    Posted 7 months ago #

    Thank you – This worked marvelously!

    I had to change:

    'post_per_page' => 3,

    To:

    'showposts' => 3,

  4. alchymyth
    The Sweeper
    Posted 7 months ago #

    my mistake - should read: 'posts_per_page'

    I'm going to correct that in my earlier reply ;-)

Reply

You must log in to post.

About this Topic