Forums

[resolved] In loop, do something with newest four posts (7 posts)

  1. bobdobbs
    Member
    Posted 5 months ago #

    I've got a loop that grabs posts of a certain category.

    Within the loop, I'd like to have something like this:

    // if this post is amongst the latest four, then do this...

    How can I do this?

  2. alchymyth
    The Sweeper
    Posted 5 months ago #

    try:

    if( $wp_query->current_post <=3 && !is_paged() ) :
    //do something//
    endif;
  3. bobdobbs
    Member
    Posted 5 months ago #

    Hi alchymyth.

    Whatever that does, it must be close.
    This is how I'm expressing the condtion:

    if ( $wp_query->current_post <=3 && !is_paged())  {
    	  echo "<li class=\"sister-post newpost \">"  ;
    	}else{
    	  echo "<li class=\"sister-post\">"  ;

    The results get paged across two pages. 8 results show on the first page. Using the code you've given me, every one of the first 8 results is given the class 'newpost'.

    Maybe I'm doing something wrong, but the condition proves true for every one of the first 8 results.

    Could you break it down a little for me?
    What is this idiom?

    <=3

  4. alchymyth
    The Sweeper
    Posted 5 months ago #

    the code is based on a 'normal' query and loop; your code might be different, in which case you might need to use a counter variable;

    example structure:

    $counter = 0; //initialize counter
    /*query and start of loop*/
    $counter++;
    if ( $counter <=4 && !is_paged())  {
    	  echo "<li class=\"sister-post newpost \">"  ;
    	}else{
    	  echo "<li class=\"sister-post\">"  ;
    }
    /*end of loop*/

    can you paste the full code of your template into a http://pastebin.com/ and post the link to it here?

  5. bobdobbs
    Member
    Posted 5 months ago #

    This is the current state of the code:
    http://pastebin.com/rzUA1bpc

    I'm afraid it's a little complicated. But in essence it does this: it grabs all posts of a certain category. If the post has an attachment, it publishes the post.

    I'm afraid a counter won't help.
    Using a counter, I can do something special to x number of post that I publish. But, I specifically want to do something special with the newest 4 posts, no matter what criteria I use to sort them by.

    So, I might want to publish posts sorted, say, author. But I'd still like to apply a particular css class to whichever posts, regardless of authorship, that where published most recently.

  6. alchymyth
    The Sweeper
    Posted 5 months ago #

    possibly:

    at the start of your code, add:

    <?php $last_four = get_posts('numberposts=4');
    $last_four_ids = array();
    foreach($last_four as $last) {
    $last_four_ids[] = $last->ID;
    } ?>

    and as conditional, try:

    if( in_array($post->ID, $last_four_ids) )  {
  7. bobdobbs
    Member
    Posted 5 months ago #

    Yes!
    That does it.

    Thanks very much.

    To add a complication, the blog has a number of categories, and I only want the condition to return true if the post being presented is of a certain category.

    I think I've nailed this by adding a parameter to the getposts call, thus:

    $last_four = get_posts('numberposts=4', 'category=4')

    My initial tests seem to show this as succesful.

    Again, thanks for helping me get this result. Much appreciated :)

Reply

You must log in to post.

About this Topic

Tags