• Guys,

    I’m trying to display a simple linkable list of posts for two years only. Per Mikos at this post he made code that uses a single DB call to grab the info. Below is the code.

    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' => 'ASC',
        '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;
    }

    he also displays this block of code and explains that it should be inserted into the theme.

    <?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; ?>

    How can this be modified so a shortcode can be used? I’m confused as to how to insert the second half of the code in the template if it is to only be shown on one page.

    Thanks in advance for any help.

    FYI: The theme used is Genesis. All plugins and core are up to date.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try this:

    function list_posts_by_year($first_date, $last_date) {
    	require_once( ABSPATH. "wp-blog-header.php" );
    	query_posts('showposts=-1');
    	if(have_posts()) {
    		while(have_posts()) : the_post();
    			$ids = get_the_ID(). ',';
    			$list = array( $ids );
    			foreach ($list as $items) {
    				$date = get_the_date("Y");
    				if($date >= $first_date && $date <= $last_date) {
    					echo "<a href=\"".get_the_permalink()."\" title=\"".get_the_title()."\">".get_the_title()."</a><br />";
    				}
    			}
    		endwhile;
    		wp_reset_query();
    	}
    }
    
    function list_posts_by_year_shortcode() {
    	ob_start();
    	// an example usage to list posts published between 2014 and 2015
    	list_posts_by_year("2014", "2015");
    	return ob_get_clean();
    }
    add_shortcode('list-posts-by-year', 'list_posts_by_year_shortcode');

    Don’t forget to update line:21 with the dates, example:
    list_posts_by_year("2014", "2015");

    Shortcode to use: [list-posts-by-year].

    Let me know if it worked for you.

    Thread Starter ScottFromPA

    (@scottfrompa)

    YES! It works great!.

    In order for 2014 to drop away when 2016 arrives means that I’ll need to modify line 21 i guess.

    What would be the code to automatically grab two years worth of posts.

    <?php
    // would this get me close?
    $current_day = date('j');
    $last_year   = date('Y')-1;
    ?>

    Great!
    This would do that:

    $first_date = date('Y');
    $last_date = $first_date - 2;

    Full code:

    function list_posts_by_year() {
    	$first_date = date('Y');
    	$last_date = $first_date - 2;
    	require_once( ABSPATH. "wp-blog-header.php" );
    	query_posts('showposts=-1');
    	if(have_posts()) {
    		while(have_posts()) : the_post();
    			$ids = get_the_ID(). ',';
    			$list = array( $ids );
    			foreach ($list as $items) {
    				$date = get_the_date("Y");
    				if($date >= $first_date && $date <= $last_date) {
    					echo "<a href=\"".get_the_permalink()."\" title=\"".get_the_title()."\">".get_the_title()."</a><br />";
    				}
    			}
    		endwhile;
    		wp_reset_query();
    	}
    }
    
    function list_posts_by_year_shortcode() {
    	ob_start();
    	list_posts_by_year();
    	return ob_get_clean();
    }
    add_shortcode('list-posts-by-year', 'list_posts_by_year_shortcode');

    Tell me if it works because in my local installation I have only few 2015 posts..

    Thread Starter ScottFromPA

    (@scottfrompa)

    Thanks for the help Samuel!

    Long live WordPress!

    You’re welcome Scott!
    haha, agree 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Post List Showing Two Current Years’ is closed to new replies.