WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] add the year to this function (3 posts)

  1. csleh
    Member
    Posted 2 years ago #

    I"ve been trying various methods of adding the year to this function, but I keep breaking the page. How can I sort by just the current year?

    <?php
    	global $post;
    	$categories = get_the_category();
    	$category = $categories[0];
    	$cat_ID = $category->cat_ID;
    	$myposts = get_posts("numberposts=20&category=$cat_ID");
    	?>
    	<?php foreach($myposts as $post) :?>
    	<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    	<?php endforeach; ?>

    After this list I'll (try to) add a link to archives for posts in this category by year.

  2. Big Bagel
    Member
    Posted 2 years ago #

    Try adding the year parameter:

    $year = date('Y');
    $myposts = get_posts( "numberposts=20&category=$cat_ID&year=$year" );
  3. csleh
    Member
    Posted 2 years ago #

    thank you! It was the &year=$year that I was missing. works a treat!

Topic Closed

This topic has been closed to new replies.

About this Topic