0

我正在运行一个自定义查询,但是当返回posts_per_page 时,它​​总是落后两个。我正在使用一种称为事件的自定义帖子类型,即拉入代码以显示函数文件中的帖子。我也想实现分页(也不工作),这会是一个问题吗?

我运行了 var dump,它返回查询中的所有内容,包括当前日期之前的帖子。我想是因为它在 if 语句之前转储了?有任何想法吗?

有任何想法吗?

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    //Current exhibitions
    $args = array(
           'post_type' => 'exhibitions',
           'orderby'   => 'exhibition_start_date',
           'meta_key'  => 'exhibition_start_date',
           'order'     => 'ASC',
           'posts_per_page' => 6,
           'paged' => $paged
       );

        $my_query = new WP_Query( $args );

      while ( $my_query->have_posts() ) : $my_query->the_post();
           $exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
           //Current

           if ($exhibition_start_date >= date('Y-m-d') ) {
               get_exhibition_container();
           }

        endwhile;

   wp_reset_postdata();
?>

新代码:(我还在我的初始循环中添加了页面内容,认为它可能会干扰?)

<div role="main" class="clearfix">  
    <div class="one-column">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <?php endwhile; else: ?>
     <?php endif; ?>

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    //Current exhibitions
    $args = array(
           'post_type' => 'exhibitions',
           'meta_query' => array(
           array (
                    'key' => 'exhibition_start_date',
                    'value' => date('Y-m-d'),
                    'compare' => '>='
                 )
            ),
           'orderby'   => 'exhibition_start_date',
           'meta_key'  => 'exhibition_start_date',
           'order'     => 'ASC',
           'posts_per_page' => 10,
           'paged' => $paged
       );

        $my_query = new WP_Query( $args );


      while ( $my_query->have_posts() ) : $my_query->the_post();
           $exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
           //Current
            get_exhibition_container();


        endwhile;

   wp_reset_postdata();
?>

<div class="previous-holder">
<?php previous_posts_link(); ?>
</div>
<div class="next-holder">
<?php next_posts_link(); ?>
</div>
4

1 回答 1

0

而不是在循环中使用if条件,您应该使用meta_queryparam :

$args = array(
    'post_type' => 'exhibitions',

    'meta_query' => array(
        array(
            'key' => 'exhibition_start_date',
            'value' => date('Y-m-d'),
            'compare' => '>='
        )
    ),

    'orderby'   => 'exhibition_start_date',
    'meta_key'  => 'exhibition_start_date',
    'order'     => 'ASC',

    'posts_per_page' => 6,
    'paged' => $paged
);

编辑:关于分页

previous_posts_link法典

打印指向当前查询中上一组帖子的链接。

您应该尝试使用previous_posts_link()and next_posts_link()beforewp_reset_postdata()

于 2012-10-08T10:07:40.170 回答