WordPress,你有时会激怒我!
在过去的几个小时里,我一直在尝试做的是试图找到一种方法,让我可以使用自定义日期格式(不更改设置中的全局选项)在侧边栏显示存档,这是一项非常复杂的任务,应该真的就像使用 the_date(); 一样简单。例如,我只想制作 2013 年 1 月,而不是 1 月 13 日,同时还有一年在一个列中,另一个在另一列中。
通过调整此代码,我已经成功地完成了自定义日期格式:http: //www.wpbeginner.com/wp-themes/how-to-customize-the-display-of-wordpress-archives-in-your-侧边栏/对此:
function sidebar_archive_list($arclistno = "24") {
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){ }
}
?>
<a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>" title="<?php echo date("F Y", mktime(0, 0, 0, $month->month, 1, $month->year)); ?>"><?php echo date("M y", mktime(0,0,0,$month->month,1,$month->year)); ?></a>
<?php
$year_prev = $year_current;
if(++$limit >= $arclistno) { break; }
endforeach;
}
这工作没有问题。
当我尝试将此函数返回的内容拆分为两个单独的列 div 时,问题就来了。我相信我需要将函数变成一个数组,就像你可以从 wp_get_archives('echo=0'); (还有一些额外的代码),但我的 PHP 技能并不高超,而且我无法完全弄清楚如何重新创建相同的东西。我认为它应该看起来像:
array(3) { [0]=> string(86) "January 2013"
[1]=> string(90) "September 2012"
[2]=> string(82) "March 2012"
}
当 var_dumped 时。同样值得牢记的是,无论出于何种原因,锚链接也围绕日期,但会回显,而不是打印出来。
我用于内容拆分的代码是:
<?php
$links = print(sidebar_archive_list());
$archive_n = count($links);
for ($i=0;$i<$archive_n;$i++):
if ($i<$archive_n/2):
$archive_left = $archive_left.'<li>'.$links[$i].'</li>';
elseif ($i>=$archive_n/2):
$archive_right = $archive_right.'<li>'.$links[$i].'</li>';
endif;
endfor;
?>
<div class="group">
<ul class="sb_double_column_list alignleft">
<?php echo $archive_left;?>
</ul>
<ul class="sb_double_column_list alignright">
<?php echo $archive_right;?>
</ul>
</div>
任何帮助或建议将不胜感激。我对此很着迷!