敏敏如是 發表於 2023-10-17 00:00:00

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-&gt;posts WHERE `post_type` = 'post' AND `post_status` = 'publish' AND YEAR(post_date) = $year ORDER BY `month` DESC";<br>
$months = $wpdb-&gt;get_results($query);<br>
echo '&lt;ul&gt;';<br>
echo '&lt;li&gt;&lt;a href="'.get_year_link( $year ).'"&gt;'.sprintf('%d', $year).'&lt;/a&gt;';<br>
echo '&lt;ul&gt;';<br>
foreach( $months as $month ) {<br>
$text = sprintf(__('%1$s'), $wp_locale-&gt;get_month($month-&gt;month));<br>
echo '&lt;li&gt;&lt;a href="'.get_month_link( $year, $month-&gt;month).'"&gt;'.$text.'&lt;/a&gt;&lt;/li&gt;';<br>
}<br>
echo '&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;';</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;">
&lt;ul&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/"&gt;2013&lt;/a&gt;<br>
&lt;ul&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/11/"&gt;十一月&lt;/a&gt;&lt;/li&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/09/"&gt;九月&lt;/a&gt;&lt;/li&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/08/"&gt;八月&lt;/a&gt;&lt;/li&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/06/"&gt;六月&lt;/a&gt;&lt;/li&gt;<br>
&lt;li&gt;&lt;a href="http://yourdomain.com/2013/02/"&gt;二月&lt;/a&gt;&lt;/li&gt;<br>
&lt;/ul&gt;<br>
&lt;/li&gt;<br>
&lt;/ul&gt;</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]
查看完整版本: WordPress查询某年某月文章的SQL语句