嵌套的年/月/帖子标题存档
Posted
技术标签:
【中文标题】嵌套的年/月/帖子标题存档【英文标题】:Nested Year/Month/Post title archive 【发布时间】:2013-01-28 10:42:09 【问题描述】:我正在尝试在 wordpress 主题中为我的新闻部分创建一个嵌套的帖子存档:
<div class="blog-list-archive">
<?php
/**/
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date)
FROM $wpdb->posts WHERE post_status = 'publish'
AND post_type = 'post' ORDER BY post_date DESC");
foreach($years as $year) :
?>
<li><a href="javascript:void()"><?php echo $year; ?></a>
<ul class="archive-sub-menu">
<? $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date)
FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'
AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
foreach($months as $month) :
?>
<li><a href="<?php echo get_month_link($year, $month); ?>">
<?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
</li>
<?php endforeach;?>
</ul>
</li>
<?php endforeach; ?>
</div>
如何扩展它以显示每个月的帖子标题?另外我只想获取“新闻”类别“13”下的帖子。
只是为了说明我需要这种格式:
2013
一月 帖子标题 1 帖子标题2 帖子标题 32012
十二月 帖子标题 1 帖子标题2 帖子标题 3 11 月 帖子标题 1 帖子标题2【问题讨论】:
【参考方案1】:将此代码放在打印月份名称的代码下方。
global $wpdb;
$sposts = $wpdb->get_col( "
SELECT ID
FROM $wpdb->posts
WHERE MONTH(post_date) = '$month'
AND YEAR(post_date) = '$year'
AND post_status = 'publish'
AND 'post_type' = 'post'
ORDER BY post_date DESC" );
foreach( $sposts as $spost )
echo '<a href="' . get_permalink( $spost ) . "'>" . get_the_title( $spost ) . "</a>";
【讨论】:
在回显之前,输入if( in_category( 13, $spost ) )
,请参阅codex.wordpress.org/Function_Reference/in_category以上是关于嵌套的年/月/帖子标题存档的主要内容,如果未能解决你的问题,请参考以下文章