我用下面的代码列出我博客中的所有年份,我想我们可以称之为归档。
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC");
foreach($years as $year) : ?>
<?php echo $year ?>
<?php endforeach; ?>
不过,我想实现的是,在每年的前面显示每年的帖子数量(count)。示例:
2015 (5 posts)
2014 (3 posts)
2011 (10 posts)
我在某处找到了一个提示,并试图实现它,但没有成功。以下是我所尝试的:
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC");
$count = $years->post_count;
foreach($years as $year) : ?>
<?php echo $year ?> (<?php echo $count ?> posts)
<?php endforeach; ?>
有什么想法吗?
非常感谢。
EDIT @David
<?php
/** Grab the years that contain published posts, and a count of how many */
$query = $wpdb->prepare(\'
SELECT YEAR(%1$s.post_date) AS `year`, count(%1$s.ID) as `posts`
FROM %1$s
WHERE %1$s.post_type IN ("post")
AND %1$s.post_status IN ("publish")
GROUP BY YEAR(%1$s.post_date)
ORDER BY %1$s.post_date DESC\',
$wpdb->posts
);
$results = $wpdb->get_results($query);
/** Create the \'$years\' array, a list of all yearly archives */
$years = array();
if(!empty($results)) : foreach($results as $result) :
$url = get_year_link($result->year); // The archive link URL for the current year
$text = $result->year; // The archive link text for the current year
$count = $result->posts; // The number of posts in the current year
$years[] = get_archives_link($url, $text, \'html\', \'\', $count); // Create the archive link for the current year
endforeach;
endif;
?>
<a href="<?php echo $url; ?>"><li><?php echo $text ?> <span class="lower">(<?php echo $count ?>)</span></li></a>
SO网友:David Gard
简单的方法显然是最简单的方法,但除非您愿意在呈现归档文件的方式上稍微灵活一点,否则这并不适合您。
使用wp_get_archives()
作用不幸的是,虽然没有可用的过滤器来操作输出,但您将看到这样一个列表。计数将不是链接的一部分-
2015(5)2014(3)2011(10)
这是密码-
$args = array(
\'type\' => \'yearly\',
\'show_post_count\' => true
);
wp_get_archives($args);
因此,如果你认为简单的方法不适合你,那就只会留下艰难的道路。本质上,我们是在复制
wp_get_archives()
, 但您可以完全按照自己的意愿格式化输出。
下面的代码使用了一个自定义查询,与您自己的查询没有什么不同(除了我们获取了帖子数量和年份),以及函数get_year_link()
和get_archives_link()
.
输出将为您留下一个列表,其格式与您在问题更新中发布的格式相同。计数将是链接的一部分-
2015(5)2014(3)2011(10)
这是密码-
/** Grab the years that contain published posts, and a count of how many */
$query = $wpdb->prepare(\'
SELECT YEAR(%1$s.post_date) AS `year`, count(%1$s.ID) as `posts`
FROM %1$s
WHERE %1$s.post_type IN ("post")
AND %1$s.post_status IN ("publish")
GROUP BY YEAR(%1$s.post_date)
ORDER BY %1$s.post_date\',
$wpdb->posts
);
$results = $wpdb->get_results($query);
/** Create the \'$years\' array, a list of all yearly archives */
$years = array();
if(!empty($results)) : foreach($results as $result) :
$count = \'<span class="lower">(\' . $result->posts . \')</span>\'; // The number of posts in the current year
$url = get_year_link($result->year); // The archive link URL for the current year
$text = \'<li>\' . $result->year . \' \' . $count . \'</li>\'; // The archive link text for the current year
$years[] = get_archives_link($url, $text, \'html\'); // Create the archive link for the current year
endforeach;
endif;
/** Output the custom archive list */
echo join("\\n", $years);
编辑根据你的评论,我对上面的回答做了一些修改。
之所以您只能输出一年的数据,是因为您只清楚地重复了foreach
环代码所做的是每年将$years
数组,然后使用最终的行-echo连接(“\\n”,$年)输出它们;