WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Targeting latest post in loop that is not sticky (9 posts)

  1. Boris
    Member
    Posted 5 years ago #

    Hey,

    so, I've got a normal WP loop on my index.php file. I've got three images, that I want to attach to the posts depending on their status (sticky, latest post, sticky & latest post at the same time).

    I use this to attach the sticky image:
    <?php if (is_sticky()) { echo '<span class="featured"></span>'; } ?>

    Now I've got a problem. I can't figure out how to target the latest non-sticky post.

    Once I've got that, attaching the third image to sticky/latest posts isn't gonna be a problem.

    Any help is greatly appreciated.

  2. MichaelH
    Member
    Posted 5 years ago #

    Couldn't you just add an 'else' to your 'if' and in that else do a $count?

    <?php
    $count=0;
    if (is_sticky()) {
    echo '<span class="featured"></span>';
    } else {
    $count++;
    if (1 == $count) {
    //print first non-sticky post
    }
    }
    ?>
  3. Boris
    Member
    Posted 5 years ago #

    Cheers for your answer, Michael.

    That works to a certain extent. But when the latest post is being made sticky, then latest-post-image is being assigned to the second latest post.

    It shouldn't be displayed at all. Rather, the now latest/sticky post needs to get a new label assigned <span class="feat-sticky"></span>

  4. MichaelH
    Member
    Posted 5 years ago #

    Okay give me your rules in english

    if the post is the first sticky then do this
    if the post is the first sticky and the latest post do this

    etc.

  5. Boris
    Member
    Posted 5 years ago #

    if a post is sticky display the stickypost-image

    if a post is the latest post display the latestpost-image

    if a post is both the latest post and sticky display the lateststickypost-image

    display nothing for all other posts.

    That's basically it.

  6. MichaelH
    Member
    Posted 5 years ago #

    Please this before your loop:

    <?php
    $count=0;
    ?>

    Then this:

    <?php
    $count++;
    if (is_sticky()) {
      if (1 == $count) {
        //display the lateststickypost-image
        } else {
        //display the stickypost-image
        }
      } else {
        if (1 == $count) {
        //display latestpost-image
      }
    }
    ?>
  7. Boris
    Member
    Posted 5 years ago #

    Hey,

    thanks again. It still doesn't work exactly right. When I have a sticky post, that is not the latest post, it shows the sticky/latest-post image. In that case the latest post doesn't get an image.

    All other cases work correctly, though.

  8. MichaelH
    Member
    Posted 5 years ago #

    Actually thinking about it, sticky posts will always appear at the top of your loop so determining whether a stick post is the latest post can't really done by a counter. To do it truly right, would have to loop through the posts to find the latest post then check the date of the sticky post against the date of the sticky post to see if it truly is the latest.

  9. Boris
    Member
    Posted 5 years ago #

    Alright, got it to work. Here's what I did in case somebody's trying to do something similar.

    First I placed this bit before the loop:

    <?php
    global $wpdb;
    $queries = $wpdb->get_results("SELECT ID, post_date FROM $wpdb->posts WHERE post_type='post' ORDER BY post_date DESC LIMIT 1 ");
    $date = $queries[0]->post_date;
    ?>

    This gets the post date of the first post out of the database. Then I placed this inside the loop:

    <?php
    global $post;
    $post = get_post($post);
    $time = $post->post_date;
    ?>

    This gets the post date of every post inside the loop. Then, again inside the loop, I put this bit to display the various images by checking if a post is sticky and comparing the dates:

    <?php if(is_sticky() && ($date == $time)){
    	echo '<span class="feat-sticky"></span>';
    } elseif(is_sticky()) {
    	echo '<span class="featured"></span>';
    } elseif($date == $time){
    	echo '<span class="latest"></span>';
    } else {
    // Do nothing
    } ?>

    Not sure if this is really the way to do this, but it works. Thanks, Michael for helping me figuring this out.

Topic Closed

This topic has been closed to new replies.

About this Topic