【发布时间】:2014-02-13 14:16:24
【问题描述】:
我想出的解决方案遇到了一些困难。
我正在使用 get_adjacent_posts 函数在循环中返回上一篇文章
<div class="timeline">
<?php $args = array('post_type' => 'case', 'cases' => $term->slug, 'order' => 'DEC', 'orderby' => 'meta_value', 'meta_key' => 'wpcf-date'); $query = new WP_Query($args); while ($query->have_posts()) : $query->the_post(); ?>
<?php
$previous_post = get_adjacent_post(true, '', true, 'cases'); //Get The previous post
$prev_year = date("Y", get_post_meta($previous_post->ID, 'wpcf-date', true)); //Get the year of the previous post
$entry_year = date("Y", (types_render_field("date", array("raw"=>"true")))); //Get the year of the current post
if ($prev_year != $entry_year) {
?>
<strong class="entry-year"><?php echo $entry_year; ?></strong>
<?php } ?>
<div class="timeline-entry">
<span class="entry-date"><?php echo types_render_field("date", array("raw"=>"false")); ?></span>
<span class="entry-citation"><?php echo types_render_field("citation-court", array("raw"=>"false")); ?></span>
<strong class="entry-title"><?php the_title(); ?></strong>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
基本上,我想要实现的是显示帖子的“时间线”,并且每个具有相同年份的帖子块只回显一次年份(自定义字段)。除了 get_adjacent_post 函数不遵守在我的原始查询中应用的排序这一事实之外,我的解决方案还在工作。我需要按元键 'wpcf-date' 对 get_adjacent_post 函数进行排序。
我相信可以过滤 get_adjacent_posts 以输出我想要的内容,但我不知道该怎么做。如果有人可以帮助我,将不胜感激!
【问题讨论】: