Wordpress - 如何仅显示存档列表中有帖子的年份和月份
Wordpress - How to show only years and months that have posts in archive list
我有这个存档列表,按年和月显示我的帖子。但它显示没有帖子的年份和月份。我怎样才能只显示实际有帖子的月份和年份?谢谢。
<?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC"); ?>
<?php foreach($years as $year) : ?>
<span class="anni"><?php echo $year; ?></span>
<ul style="list-style-type:none;">
<?php $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC"); ?>
<?php foreach($months as $month) : ?>
<li><h3 class="mesi"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></h3>
<ul>
<?php $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND MONTH(post_date)= '".$month."' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC"); ?>
<?php foreach ($theids as $theid): ?>
<li class="archivelist">
<div style="width:150px;height:170px;float:left;margin:10px;">
<a href="<?php bloginfo('url'); ?>?p=<?php echo $theid->ID; ?>">
<?php if (strlen($theid->post_title) > 15) :?>
<?php echo substr($theid->post_title, 0, 15) . '...'; ?>
<?php else : ?>
<?php echo $theid->post_title; ?>
<?php endif; ?>
<?php if (has_post_thumbnail($theid->ID) ) : ?>
<?php echo get_the_post_thumbnail( $theid->ID, 'thumbnail' ); ?>
<?php else : ?>
<?php echo '<img style="width:150px;" src="wp-content/themes/twentythirteen-child/images/noimg.jpg"/>' ;?>
<?php endif; ?>
</a>
</div>
</li>
<?php endforeach; ?>
</ul> <br style="clear:left;">
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
显然,几个月内有 'post' 以外类型的帖子,不应显示。
尝试包括
AND post_type = 'post'
进入用于选择年份和月份的查询中的 WHERE 子句。
我是这样做的:
SELECT JAL.year, JAL.month, COUNT(JAL.ID) as `posts` FROM (" .
"SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`,ID " .
"FROM {$wpdb->posts} {$join} {$where}" .
") JAL GROUP BY JAL.year,JAL.month ORDER BY JAL.year,JAL.month DESC
您对所有帖子进行 select,然后提取其中的年份和月份,然后按年份和月份对它们进行分组。
我有这个存档列表,按年和月显示我的帖子。但它显示没有帖子的年份和月份。我怎样才能只显示实际有帖子的月份和年份?谢谢。
<?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC"); ?>
<?php foreach($years as $year) : ?>
<span class="anni"><?php echo $year; ?></span>
<ul style="list-style-type:none;">
<?php $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC"); ?>
<?php foreach($months as $month) : ?>
<li><h3 class="mesi"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></h3>
<ul>
<?php $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND MONTH(post_date)= '".$month."' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC"); ?>
<?php foreach ($theids as $theid): ?>
<li class="archivelist">
<div style="width:150px;height:170px;float:left;margin:10px;">
<a href="<?php bloginfo('url'); ?>?p=<?php echo $theid->ID; ?>">
<?php if (strlen($theid->post_title) > 15) :?>
<?php echo substr($theid->post_title, 0, 15) . '...'; ?>
<?php else : ?>
<?php echo $theid->post_title; ?>
<?php endif; ?>
<?php if (has_post_thumbnail($theid->ID) ) : ?>
<?php echo get_the_post_thumbnail( $theid->ID, 'thumbnail' ); ?>
<?php else : ?>
<?php echo '<img style="width:150px;" src="wp-content/themes/twentythirteen-child/images/noimg.jpg"/>' ;?>
<?php endif; ?>
</a>
</div>
</li>
<?php endforeach; ?>
</ul> <br style="clear:left;">
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
显然,几个月内有 'post' 以外类型的帖子,不应显示。
尝试包括
AND post_type = 'post'
进入用于选择年份和月份的查询中的 WHERE 子句。
我是这样做的:
SELECT JAL.year, JAL.month, COUNT(JAL.ID) as `posts` FROM (" .
"SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`,ID " .
"FROM {$wpdb->posts} {$join} {$where}" .
") JAL GROUP BY JAL.year,JAL.month ORDER BY JAL.year,JAL.month DESC
您对所有帖子进行 select,然后提取其中的年份和月份,然后按年份和月份对它们进行分组。