WordPress.org

Ready to get started?Download WordPress

Forums

How to display latest posts from first cat id?? (11 posts)

  1. jesulliv
    Member
    Posted 3 years ago #

    Hi. I'm helping to set up a blog and on our single post pages we want to display the latest 2 posts from that category (excluding the current) in the sidebar.

    Since I want it on all single pages, I can't specify the name or id. And to make matters even more complicated, almost all posts are in multiple categories.

    So what I want it something that outputs the latest 2 from the first cat id only, excluding the current post.

    I know how to do all of these things singularly, but I am having a lot of trouble putting them all together. Can anyone help me out? Thanks in advance.

  2. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    I would have a look through the examples on this page: http://codex.wordpress.org/Template_Tags/query_posts

    Look at the 'offset' parameter to skip the first post.

    You can create and use a second query after the "single" post is displayed (remember to reset the query afterward, just in case). This normally works fine as the 'sidebar' is generally called after the content is displayed.

    This might get you started:

    query_posts( "cat=$cat_choice&posts_per_page=2&offset=1" );
  3. jesulliv
    Member
    Posted 3 years ago #

    Thanks. That displays the 2 latest posts, excluding the current. But it is not displaying from the correct cat.

    This is where I keep getting stuck. Each post has two cat ids. I need only posts in the first category displayed. (I am assuming "first" is the lower cat id number.)

  4. jesulliv
    Member
    Posted 3 years ago #

    Can anyone confirm that wp recognizes the lower number as the first cat id? I should probably make sure I have that bit straight before I try anything else.

    I've been searching on the subject and can't seem to find a straight answer.

  5. esmi
    Forum Moderator
    Posted 3 years ago #

    Can anyone confirm that wp recognizes the lower number as the first cat id?

    In terms of functions like the_category() etc., yes. That's confirmed.

  6. jesulliv
    Member
    Posted 3 years ago #

    Thank you for clarifying that.

    OK, so given that I am setting up the categories and thus can be sure that the category I want excluded will always have the higher number, how can I show only posts from the first, or lower ID, category???

  7. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    Try something like this:

    $category = get_the_category();
    query_posts( "cat=$category[0]->cat_ID&posts_per_page=2&offset=1" );
  8. jesulliv
    Member
    Posted 3 years ago #

    Catchable fatal error: Object of class stdClass could not be converted to string...

    Changed the double quotes to single and it displays 2 posts, but one is the current post and the other isn't from the right cat.

  9. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    <?php
    $category = get_the_category( $post->ID );
    $cat_choice = $category[0]->cat_ID;
    query_posts( "cat=$cat_choice&posts_per_page=2&offset=1" );
    if ( have_posts() ) : while ( have_posts() ) : the_post();
      // the content et al.
    endwhile; else :
      // no posts etc.
    endif;
    wp_reset_query();
    ?>

    Not much more I can do with that ... you will have to fill in the blanks from there.

    PS: Although tested to insure it works (pasted into a sidebar.php file in a random theme on one of my test sites) your mileage may vary. EAC

  10. jesulliv
    Member
    Posted 3 years ago #

    It worked!!! Thank you so much, Edward.

    I've been trying to figure this one little sidebar piece for the better part of the day.

    Thank you for saving me much time and frustration. If you are ever in North Carolina, beers on me. :)

  11. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    Thank You! It was an interesting problem to sort out; glad it's working for you.

Topic Closed

This topic has been closed to new replies.

About this Topic