我希望按年显示帖子链接组列表。存档功能不这样做 例如:
2010
Blender 2.53 released
Gamequery 0.5 released
synfig 0.62.02 released
...
2009
Gimp 2.67 released
Blender 2.52 released
我该怎么做?
答案 0 :(得分:8)
查询所有帖子,按帖子日期排序(我假设下降?)。你一个接一个地打印,记住年份,当前发布的年份与上一年的年份不同,你知道你必须打印年份标签。
query_posts(array('nopaging' => 1, /* we want all posts, so disable paging. Order by date is default */));
$prev_year = null;
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$this_year = get_the_date('Y');
if ($prev_year != $this_year) {
// Year boundary
if (!is_null($prev_year)) {
// A list is already open, close it first
echo '</ul>';
}
echo '<h3>' . $this_year . '</h3>';
echo '<ul>';
}
echo '<li>';
// Print the link to your post here, left as an exercise to the reader
echo '</li>';
$prev_year = $this_year;
}
echo '</ul>';
}
答案 1 :(得分:2)
您可能需要尝试以下代码:
wp_get_archives(array('type' => 'yearly'));
还有几个参数可供尝试。有关详细信息,请参阅http://codex.wordpress.org/Function_Reference/wp_get_archives。
答案 2 :(得分:1)
我认为您必须制作自己的自定义查询才能按年获得帖子。像这样:
$query = $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' group by postyear", $wpdb->siteid );
注意:我把“group by postyear”放在哪里,我不确定该列名是什么,所以你必须弄清楚它是如何存储在你的博客表中的。这可能是您必须从日期字段中解析出年份的情况。试一试。然后你可以像这样格式化你的输出:
$terms = $wpdb->get_results($query,ARRAY_A);
echo "<ul>";
foreach($terms as $detail)
{
echo "<li style='font-size:1.3em;'><a href='http://".$detail[ 'domain' ].$detail['path']."'>".get_blog_option( $detail['blog_id'], 'blogname' )."</a></li>";
}
echo "</ul>";