Support » Fixing WordPress » Single – Listing posts from one current category

  • Resolved jumust

    (@jumust)


    Hi,
    I’m using the code below and it gets posts from current category in single post sidebar. The posts I’m talking about have multiple categories (2 cat) assigned. It gets posts from first category if I set $category[0] and second category if I set $category[1].

    Also by default it gets categories sorting them by name and not by ID.

    One of those category is a “Featured” cat and I don’t want to show posts from this category, but it happens when the second category has as first letter G, H, I, etc…. Also I set up this category with an ID = 9999 so it’s always the last category.

    Is there a way to show posts only from lower category ID number between multiple categories assigned?

    <h3>Recently Post From Same Category</h3>
    <ul>
    <?php
    global $post;
    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;
    $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish'));
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
    <li>
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?></a>
    </li>
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>
    </ul>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Here is an article describing my solution to a similar problem: http://wordpress.mcdspot.com/2010/06/02/stay-in-category/

    Thanks for sharing this but I can’t find a solution, I’m not so good at coding.

    I thought that it might be easier than what you explained, basically just like doing a query for current posts in single post sidebar and exclude one category always (let’s say the number 1), so if the post belongs to number 1 and 2 category it will display only posts from number 2 category.

    If post belong only to number 3 category it will show posts from 3 category.

    Maybe I can use the category__not_in..???

    If all you want is the lowest category id, you can sort the $category array like this:

    <h3>Recently Post From Same Category</h3>
    <ul>
    <?php
    function sort_category($a, $b) {
       $a_id = $a->term_taxonomy_id;
       $b_id = $b->term_taxonomy_id;
       return ($a_id == $b_id) ? 0 : (($a_id > $b_id) ? 1 : -1);
    }
    global $post;
    $category = get_the_category($post->ID);
    usort($category, 'sort_category');
    $category = $category[0]->cat_ID;
    $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish'));
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
    <li>
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?></a>
    </li>
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>
    </ul>

    AWESOME thanks man! It worked

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Single – Listing posts from one current category’ is closed to new replies.