WordPress查询某年某月文章的SQL语句
<div id="content" style="margin: 0px auto; padding: 0px; outline: none; width: 700px; line-height: 25.2px; clear: both; font-size: 14px; word-wrap: break-word; word-break: break-all; overflow: hidden;"><p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
利用$wpdb自定义查询可以调用某年下的月份归档, 例如调用2014年月份归档的代码如下所示。<br>
</p>
<div style="margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; font-size: 12px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden;">
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode16" style="margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word;">
// Get archive by year<br>
global $wpdb, $wp_locale;<br>
$year = 2014;<br>
$query = "SELECT DISTINCT MONTH(post_date) AS `month` FROM $wpdb->posts WHERE `post_type` = 'post' AND `post_status` = 'publish' AND YEAR(post_date) = $year ORDER BY `month` DESC";<br>
$months = $wpdb->get_results($query);<br>
echo '<ul>';<br>
echo '<li><a href="'.get_year_link( $year ).'">'.sprintf('%d', $year).'</a>';<br>
echo '<ul>';<br>
foreach( $months as $month ) {<br>
$text = sprintf(__('%1$s'), $wp_locale->get_month($month->month));<br>
echo '<li><a href="'.get_month_link( $year, $month->month).'">'.$text.'</a></li>';<br>
}<br>
echo '</ul></li></ul>';</div>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
</p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
<br>
输出如下:<br>
</p>
<div style="margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; font-size: 12px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden;">
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode17" style="margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word;">
<ul><br>
<li><a href="http://yourdomain.com/2013/">2013</a><br>
<ul><br>
<li><a href="http://yourdomain.com/2013/11/">十一月</a></li><br>
<li><a href="http://yourdomain.com/2013/09/">九月</a></li><br>
<li><a href="http://yourdomain.com/2013/08/">八月</a></li><br>
<li><a href="http://yourdomain.com/2013/06/">六月</a></li><br>
<li><a href="http://yourdomain.com/2013/02/">二月</a></li><br>
</ul><br>
</li><br>
</ul></div>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
</p>
<div style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px;">
</div>
</div>
<div style="margin: 8px 0px 10px; padding: 0px; outline: none; width: 700px; overflow: hidden;">
<ul style='margin: 0px; padding-right: 0px; padding-left: 0px; outline: none; list-style: none; max-width: 500px; line-height: 12px; font-family: tahoma, arial, "Microsoft YaHei";'></ul>
</div>
<p>
</p>
頁:
[1]