T
T
TopdiR2013-12-18 15:29:52
PHP
TopdiR, 2013-12-18 15:29:52

How to display a WordPress category template with post division by year?

Hello dear Toaster users!
I need to display a WordPress category page like this:
---2013---
Post title 7
Post title 6
---2012---
Post title 5
Post title 4
Post title 3
Post title 2
---2011---
Title entries 1
Displaying the title and date of each individual entry is not a problem. But how to make it so that the year is displayed and under it the list of records of this year - I do not know.
If anyone knows how to do this - I will be grateful for the hint.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Zelenin, 2013-12-19
@TopdiR

<?php
$year = '';
if (have_posts()) : while (have_posts()) : the_post();
$current_year = get_the_date( 'Y' ); 
if ( $year != $current_year ) echo '<h4>---' . $current_year . '---</h4>';
?>
  
    <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
                            
        <h2><?php the_title(); ?></h2>
                            
    </article>				
<?php
$year = $current_year;
endwhile; endif; ?>

A
Alexander Zelenin, 2013-12-18
@zelenin

records are displayed in a loop. Before the loop, initialize the variable $year = '';
Further pseudocode:

$year = '';
while ( ....  ) :
$current_year = get_the_date( 'Y' );
if ( $year != $current_year ) {
echo '<h4>---' . $current_year . '---</h4>';

// .....  
}
$year = $current_year;

endwhile;

T
TopdiR, 2013-12-19
@TopdiR

@zelenin thank you, but something doesn't work for me. Apparently my initial knowledge in php makes itself felt. Here is my loop (loop) that outputs titles:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>		
  <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
              
    <h2><?php the_title(); ?></h2>
              
  </article>				
<?php endwhile; endif; ?>

I would be very grateful if you could help me insert the pseudocode you suggested correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question