WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] add the year to this function

[Resolved] add the year to this function

  • csleh
    Participant

    @csleh

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Big Bagel
    Member

    @big-bagel

    Try adding the year parameter:

    $year = date('Y');
    $myposts = get_posts( "numberposts=20&category=$cat_ID&year=$year" );
    csleh
    Participant

    @csleh

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] add the year to this function’ is closed to new replies.