WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] One List Per Year Archive (3 posts)

  1. dutchcoder
    Member
    Posted 1 year ago #

    Hi all,

    I'm actually new to coding WordPress themes and I have been stuck with the following.

    What I want to achieve is to create an archive page like so:

    [2013]
    [March]
    [25] Post
    [23] Post
    [9] Post
    [February]
    []
    []
    []

    [2012]
    [December]
    [28] Post

    Etcetera; basically, I want to separate content per year and then per month and date. However, I am stuck trying to create the functionality that makes the script skip a year in the foreach() loop if it has already been defined, i.e. what I end up getting every time is:

    [2013]
    [March]
    ...

    [2013]
    [February]
    ...

    This is not what I am after. The script I'm using is:

    <?php while ( have_posts() ) : the_post() ?>
    <?php $posts = get_posts();
    foreach($posts as $post) :
    ?>
    <tr class="year">
    <th><br /><br /><?php the_time('Y'); ?></th>
    </tr>

    Now, I know that I should be creating a conditional to check whether the year of the current post has already been displayed. I thought I'd do this with an array, but checking for the condition seems impossible with the_time():

    <?php while ( have_posts() ) : the_post() ?>
    <?php $posts = get_posts();
    $display = array();
    foreach($posts as $post) :
    if ( in_array ( the_time('Y'), $display ) {
    echo the_time('Y');
    } else {
    array_push ( the_time('Y'), $display );
    ?>

    Somehow, this always displays the year for every post I have so this seems to be the wrong path. How can I solve this design problem? I am at a loss as to how to proceed.

    Thanks in advance. Dutch

  2. bcworkz
    Member
    Posted 1 year ago #

    I didn't want to expend too much of my very limited mental capacity analyzing your script, but it appears workable in theory. Your primary mistake is assuming the_time() returns a value like most PHP functions. It doesn't! It displays a time. To get a value returned, use get_the_time() .

    You never want to echo the_time(); , just the_time(); will suffice. You cannot simply get_the_time();, you must echo get_the_time(); You will see this sort of distinction with many WP functions. A function of the form simply the_*() often displays something. If that is the case, there is invariably a related function of the form get_the_*() which returns a value instead of displaying it.

    Welcome to WordPress coding :)

  3. dutchcoder
    Member
    Posted 1 year ago #

    Cheers buddy! It seems I've got things working now; who knew appending the_time with get_ would easily solve my problem.

    $displayed = array();
    
    foreach($posts as $post) :
    
    if ( !in_array ( get_the_time('Y'), $displayed ) ) {
    ?>
    
    <tr class=year>
        <th><br /><br /><?php the_time('Y'); ?></th>
    </tr>
    
    <?php
    array_push ( $displayed, get_the_time('Y') );
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags