在做了更多的工作之后,我开发了一个解决方案,可以按年、按月、按日输出档案,同时最大限度地减少数据库调用。在此处发布供其他人使用:
//* 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
}
道具到
davidmh 和
mikos.