【问题标题】:Sorting get_adjacent_post by meta key value - Wordpress按元键值排序 get_adjacent_post - Wordpress
【发布时间】: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 以输出我想要的内容,但我不知道该怎么做。如果有人可以帮助我,将不胜感激!

【问题讨论】:

    标签: php wordpress filter


    【解决方案1】:

    你可以使用

    previous_posts_link('previous',$query->max_num_pages)
    

    已编辑:-

    function get_previous_post_id($wp_query) {
       if ($wp_query->current_post == $wp_query->post_count) return 0;
    $previous_post = $wp_query->posts[$wp_query->current_post - 1];
    return $previous_post ->ID;
    }
    

    在你的functions.php和

    中使用它
    $previous_post_id=get_previous_post_id($query);
    

    关于您需要上一个帖子 ID 的位置。 $query 是您从 Wp_query 生成的字符串

    附言最后一个帖子应该返回0;

    【讨论】:

    • 我不想要上一篇文章的链接我只需要获取上一篇文章的ID。
    • 它已被再次编辑.. 这可能会为您已经运行的 Wp_query 解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    相关资源
    最近更新 更多