检索查询中的下一篇文章

时间:2016-11-18 16:35:46

标签: php html wordpress

试图在WP帖子的查询中输出接下来的两个帖子,我想我有点卡住了。我试图检索帖子的ID,如果找到当前帖子,输出查询中的下两个帖子,但似乎我只输出后面的两个帖子。

<?php
     $test_args = array(
       'post_type' => 'hsm_testimonial',
       'posts_per_page' => 3,
       'order' => 'DESC',       
     );
     $current_post = get_the_ID(); 
     $test_query = new WP_Query( $test_args );
     if ($test_query->have_posts() ): 
     while ($test_query->have_posts() ): $test_query->the_post(); 
     if ($current_post == get_the_ID() ) : ?>           
    <?php else : ?>
        <div class="wellness-item flexbox flex-center flex-column">
            <hr class="line">
            <h3 class="text-center font-serif weight-semibold"><?php the_title(); ?></h3>
            <div class="text-center text-xxl font-serif weight-bold">&ldquo;</div>
            <div class="text-center testimonial"><?php echo get_the_excerpt(); ?> </div>
            <a href="<?php the_permalink(); ?>" class="text-center center-block">Read More</a>
            <div class="text-xxl text-center font-serif weight-bold">&rdquo;</div>
        </div>
<?php endif; endwhile; $test_query->reset_postdata(); endif; $test_query->reset_query();  ?>

任何帮助/输入都将非常有帮助。先感谢您。

1 个答案:

答案 0 :(得分:0)

如果我理解你的话,你想获得3个下一个hsm_testimonials。 尝试使用posts_where过滤器白色的东西。

代码示例:

<?php
    //Where id is greater then
    function filter_id_greater_then($where){
        if( get_the_ID() ){
            $where .= " AND ID >".get_the_ID();            
        }
        return $where;
    }

    //Query args
     $test_args = array(
       'post_type' => 'hsm_testimonial',
       'posts_per_page' => 3,
       'order' => 'DESC',       
     );

     //The query
     add_filter( 'posts_where', 'filter_id_greater_then');
     $test_query = new WP_Query( $test_args );
     remove_filter('posts_where' , 'filter_id_greater_then');
?>

<?php if ($test_query->have_posts() ): ?>

    <!--The loop-->
    <?php while ($test_query->have_posts() ): $test_query->the_post(); ?>
        <div class="wellness-item flexbox flex-center flex-column">
            <hr class="line">
            <h3 class="text-center font-serif weight-semibold"><?php the_title(); ?></h3>
            <div class="text-center text-xxl font-serif weight-bold">&ldquo;</div>
            <div class="text-center testimonial"><?php the_excerpt(); ?> </div>
            <a href="<?php the_permalink(); ?>" class="text-center center-block">Read More</a>
            <div class="text-xxl text-center font-serif weight-bold">&rdquo;</div>
        </div>
    <?php endwhile; ?>
    <!--End of the loop-->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>