WP_Query参数不起作用?

时间:2015-01-27 19:49:45

标签: wordpress-theming wordpress

所以我试图让我的wordpress网站分页正确,所以类别和页面显示正确的帖子:

    wp_reset_query();
    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $sticky = get_option( 'sticky_posts' );
    $args = array(
        'ignore_sticky_posts' => 1,
        'posts_per_page' => 10,
        'orderby' => 'date',
        'post__not_in'        => $sticky,
        'paged'               => $paged,
    );
    $query = new WP_Query( $args );

   ?>

  <?php if (have_posts()) : ?>


  <?php while (have_posts()) : the_post(); ?>

我收到要显示的帖子但是以下参数:

'ignore_sticky_posts' => 1,
            'posts_per_page' => 10,
            'orderby' => 'date',
            'post__not_in'        => $sticky,

似乎不起作用......任何想法为什么?

1 个答案:

答案 0 :(得分:1)

除非您在代码中的其他位置覆盖全局wp_reset_query(),否则您可能无需$wp_query位于顶部。

创建自定义查询时,需要在整个循环中使用它。在你的$query案例中,我们需要在调用have_posts()the_post()max_num_pages

时使用它
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
    'ignore_sticky_posts' => 1,
    'posts_per_page' => 10,
    'orderby' => 'date',
    'post__not_in'        => $sticky,
    'paged'               => $paged,
);
$query = new WP_Query( $args );

?>

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


    <?php while ( $query->have_posts() ) : $query->the_post(); ?>

         // interact with the post here

    <?php endwhile; ?>

<?php endif; ?>

// we overwrote the global $post when called the_post(); so we need to reset that
<?php wp_reset_postdata(); ?>

// again we need to reference our custom query for our pagination
<?php
$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $query->max_num_pages
) );
?>