个性化阅读
专注于IT技术分析

WP_Query参数不起作用?

因此, 我正在尝试使我的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


很有可能你不需要顶部的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
) );
?>
赞(1)
未经允许不得转载:srcmini » WP_Query参数不起作用?

评论 抢沙发

评论前必须登录!