我正在尝试创建一个自定义归档页面,该页面按可拆分列排序(取决于分辨率)。理想的情况是在引擎盖下尽可能简单的东西。
那么:使用Wordpress归档查询重新创建类似内容的最佳方法是什么?
非常感谢。最好的方法是对数据库进行SQL查询,并获得如下结果wp_get_archives()
但现在您可以根据需要操纵结果。
在你的情况下,我会这样做,我添加了一些评论,以帮助你理解这个过程。
function get_archives_lists() {
global $wpdb, $wp_locale;
// get year month array
$monthly = $wpdb->get_results("
SELECT YEAR(post_date) AS year, MONTH(post_date) AS month
FROM $wpdb->posts
WHERE post_type=\'post\' AND post_status=\'publish\'
GROUP BY YEAR(post_date), MONTH(post_date)
ORDER BY post_date DESC
", ARRAY_A);
// Rearrange the results to be [year][] = month so it will be easy to render
$arr = [];
array_map(function($item) use (&$arr) {
$arr[$item[\'year\']][] = $item[\'month\'];
return;
}, $monthly);
// loop years
foreach($arr as $yearKey => $year) {
?>
<div class="col-sm-2">
<h2><a href="<?php echo get_year_link($yearKey); ?>"><?php echo $yearKey; ?></a></h2>
<ul>
<?php
// loop 12 for monthes
for($m = 1; $m <= 12; $m++) {
// get month name
$month = sprintf( __( \'%1$s\' ), $wp_locale->get_month( $m ) );
?>
<li>
<?php
// check if the month is in our results so we add a link to it.
if(in_array($m, $year)) {
?>
<a href="<?php echo get_month_link($yearKey, $m); ?>"><?php echo $month; ?></a>
<?php
} else {
echo $month;
}
?>
</li>
<?php
}
?>
</ul>
</div>
<?php
}
}
你可以看到我使用了bootstrap类col-sm-2
例如,您可以使用自己的类和css。我正在尝试将循环拆分为4列。我的逻辑有很大的缺陷,我的布局也有问题。这么简单的问题,但我正在努力解决。我基本上需要将每组四根柱子包装在一个容器“行”分区中。当然,剩下的任何柱子,即使少于四根,也要进行包装。<div class=\"twelve columns\"> <?php $i = 0; if (have_posts() ) : while ( have_posts() ) : the_post();