-1

I'm attempting to add a custom archives page within wordpress, outputting a complete list of archives by year and month. The code below works, more or less, but a html validator outputs errors saying their are ul elements that are unclosed.

Despite trying numerous variations and playing around with the code for hours, I cannot figure out why the ul and li nesting is buggy.

Any ideas where I'm going wrong? (The code is for use in the Genesis framework for wordpress).

add_action( 'genesis_entry_content', 'custom_page_archive_content' );
function custom_page_archive_content() {

    global $post;
    echo '<ul class="archives">';
        $lastposts = get_posts('numberposts=-1');
        $year = '';
        $month = '';
        foreach($lastposts as $post) :
            setup_postdata($post);

            if(ucfirst(get_the_time('F')) != $month && $month != ''){
                echo '</ul></li><!-- /monthly-archives -->';
            }
            if(get_the_time('Y') != $year && $year != ''){
                echo '</ul></li>';
            }
            if(get_the_time('Y') != $year){
                $year = get_the_time('Y');
                echo '<li><h2>' . $year . '</h2><ul class="monthly-archives">';
            }
            if(ucfirst(get_the_time('F')) != $month){
                $month = ucfirst(get_the_time('F'));
                echo '<li><h3>' . $month . '</h3><ul>';
            }
        ?>
            <li>
                <span class="the_date"><?php the_time('d') ?>:</span>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <span class="comments-number"><?php comments_number('(0)','(1)','(%)'); ?></span>
            </li>
        <?php endforeach; ?>
        </ul><!-- /archives -->
        <?php
}
0

After much more work, I developed a solution that outputs archives by year, month and day while minimising database calls. Posting it here for others to use:

//* Add custom archives below entry content
add_action( 'genesis_entry_content', 'custom_archives_page_content' );
function custom_archives_page_content() {

global $post;

// set post arguments
$args = array(
    'posts_per_page'    => -1,
    'orderby'       => 'post_date',
    'order'         => 'DESC',
    'post_type'     => 'post',
    'post_status'       => 'publish',
);

// get posts from wordpress
$posts = get_posts($args);

// setup an associative array with three levels, for every year, month and posts
$years = array();

foreach ($posts as $post) {

    $year  = mysql2date('Y', $post->post_date);
    $month = mysql2date('F', $post->post_date);

    // specify the position of the current post
    $years[$year][$month][] = $post;
}

// begin the archives ?>

<ul class="yearly-archives">

<?php foreach ($years as $year => $months) : // shows the years ?>

    <li><h2><?php echo $year ?></h2><ul class="monthly-archives">

    <?php foreach ($months as $month => $posts ) : // shows the months ?>

        <li><h3><?php printf("%s (%d)", $month, count($months[$month])) ?></h3><ul class="daily-archives">

        <?php foreach($posts as $post) : setup_postdata($post); // shows the posts ?>

            <li>
            <span class="date"><?php the_time('d') ?>:</span>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <span class="comments-number"><?php comments_number('(0)','(1)','(%)'); ?></span>
            </li>

        <?php endforeach; // ends foreach for $posts ?>
        </ul><!-- /daily-archives --></li>

    <?php endforeach; // ends foreach for $months ?>
    </ul><!-- /monthly-archives --></li> 

<?php endforeach; // ends foreach for $years ?>
</ul><!-- /yearly-archives -->
<?php
}

Props to davidmh and mikos.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.