• Resolved mckeever02

    (@mckeever02)


    Hi there,

    Currently I have my blog posts sorted by year. I’m looking to display the latest post outside of the list and in the ‘featured post’ section. How would I go about doing that?

    I’ve tried using offset in my function and then using another loop to only display the latest post but offset is not working. If I could figure out how to exclude the latest post from my loop that’d be it sorted.

    Here’s my function for display posts in years:

    function posts_by_year() {
      // array to use for results
      $years = array();
    
      // get posts from WP
      $posts = get_posts(array(
        'numberposts' => -1,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post',
        'post_status' => 'publish'
      ));
    
      // loop through posts, populating $years arrays
      foreach($posts as $post) {
        $years[date('Y', strtotime($post->post_date))][] = $post;
      }
    
      // reverse sort by year
      krsort($years);
    
      return $years;
    }

    And the loop:

    <?php foreach(posts_by_year() as $year => $posts) : ?>
    	<h2><?php echo $year; ?></h2>
    	<ul>
    <?php foreach($posts as $post) : setup_postdata($post); ?>
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endforeach; ?>
    </ul>
    <?php endforeach; ?>

    Any help?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Removing latest post from list of posts categorised by year’ is closed to new replies.