Categories list loop – add separator every year

I don’t know what is the purpose of having each each categories for every month because WordPress has monthly archive. You can use it.

However, it is possible that what you are going to do.

I can’t write the code here because of lack of time. But I can give a guide to develop it.

Read the comments in code section.

<?php
$args=array(
  'orderby' => 'name',
  'order' => 'ASC',
)

$year="" // Assign a oldest year for this. If your first post is published in  2011, $year=2011
?>

<?php foreach (get_categories( $args ) as $cat) : 
    $cat_post_id=""; //Get the latest post id of current category and assign it to 
    $cat_post_publish_year=""; //Then find the post date of that post and assign the YEAR of that date (Only the year)

    if($year!==$cat_post_publish_year)
    {
        echo "<hr>";// Write anything to separate it.
        $year=$cat_post_publish_year;
    }


?>

    <h3><a href="https://wordpress.stackexchange.com/questions/246750/<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a></h3>
    <a href="https://wordpress.stackexchange.com/questions/246750/<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>

<?php endforeach; ?>

Leave a Comment