如何通过 'year' 然后 'months' 在 Wordpress 中显示 "Archives" 小部件布局(边栏)?
How do I display the "Archives" widget layout (sidebar) in Wordpress by 'year' then by 'months'?
我在边栏存档中使用默认小部件,它当前以这种方式显示存档:
Mar 2018
Feb 2018
Jan 2018
但是,我希望它以这种方式显示:
2018
March
February
January
2017
December
November
October
其中月份是链接。我该如何实现?我该如何处理我的 sidebar.php 文件?
我找到了答案,只需在您的 sidebar.php 或您希望存档出现的任何其他地方使用它:
<?php
global $wpdb;
$limit = 0;
$year_prev = null;
$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC");
foreach($months as $month) :
$year_current = $month->year;
if ($year_current != $year_prev){
if ($year_prev != null){?>
<?php } ?>
<li class="archive-year"><?php echo $month->year; ?></li>
<?php } ?>
<li><a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>"><span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a></li>
<?php $year_prev = $year_current;
if(++$limit >= 18) { break; }
endforeach; ?>
风格随心所欲。
我在边栏存档中使用默认小部件,它当前以这种方式显示存档:
Mar 2018
Feb 2018
Jan 2018
但是,我希望它以这种方式显示:
2018
March
February
January
2017
December
November
October
其中月份是链接。我该如何实现?我该如何处理我的 sidebar.php 文件?
我找到了答案,只需在您的 sidebar.php 或您希望存档出现的任何其他地方使用它:
<?php
global $wpdb;
$limit = 0;
$year_prev = null;
$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC");
foreach($months as $month) :
$year_current = $month->year;
if ($year_current != $year_prev){
if ($year_prev != null){?>
<?php } ?>
<li class="archive-year"><?php echo $month->year; ?></li>
<?php } ?>
<li><a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>"><span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a></li>
<?php $year_prev = $year_current;
if(++$limit >= 18) { break; }
endforeach; ?>
风格随心所欲。