WordP加密,wp_query无法在第2页上工作

时间:2015-10-01 12:11:42

标签: php wordpress loops pagination wp-query

WP Pagination有问题。

之前,我的分页工作在分类页面上,并且在第2页上显示的结果与静态主页上的第1页相同。 我找到了解决这个问题的方法,3天前我的所有页面都运行得很好。

直到昨天:现在分页仍然适用于分类页面,但是在我的静态主页上,第1页有效,但第2页和更多内容返回“此类别中没有文章”,如果wp_query循环找不到结果。

没有编辑任何文件,功能与3天前相同。

PS:如果我删除第一个错误的纠正(第2页显示与第1页相同),则错误不会回来,主页第2页仍然会返回“此类别中没有文章”;

这是我的代码:

    // Define $post_type in previous included file, already checked if parameter correctly retrieved 
    $post_type = array('magicrecipe', 'post', 'yoga-article', 'feelgood', 'gastronomie', 'voyage');

    <ul class="articlesList row mw">

    <?php

    $taxQuery = array('taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => $categoryValue);

    // "Page 2 show the same than page 1" fix
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }


    if ($search) {
        $args = array(
        'posts_per_page' => 12,
        'paged'=>$paged,
        'orderby'=> 'date'
        );
        $args['s'] = $search;
    }else{
        $args = array(
            'post_type' => $post_type, 
            'posts_per_page' => 12,
            'orderby'=> 'date',
            'paged'=> $paged,
            'tax_query' => array(array($taxQuery))
        );
    }

    $loop = new WP_Query($args); 

    $count = $loop->post_count;

    // Pagination fix
    $temp_query = $wp_query;
    $wp_query   = NULL;
    $wp_query   = $loop;

    if ($count == 0) {
        echo '<p class="articles-noArticle">Il n\'y a pas encore d\'article dans cette catégorie !</p>';
    } else {

        // starting loop
        while ($wp_query->have_posts()) : $wp_query->the_post();
        ?>
        <li class="articlesList articles-article col-1-4 col-m-1-2 col-s-1-1">
            <a href="articles-link">
                <div class="articles-thumbContainer js-squareThumb">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                        <?php the_post_thumbnail(array(500, 500), array('class' => 'articles-thumb')); ?>
                    </a>
                </div>
                <h3 class="article-title"><a href="<?php echo get_post_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p class="article-excerpt"><?php the_excerpt(); ?></p>
                <a href="<?php the_permalink(); ?>" class="article-link">En savoir plus</a>
            </a>
        </li>


    <?php
    endwhile;
}
</ul>
<div class="pagination">
<?php 
    // Custom query loop pagination
    echo paginate_links();
    // wp_reset_query();
?>
</div>

感谢您的帮助,我到处搜索但发现了这个问题。

1 个答案:

答案 0 :(得分:0)

你的条件太多了。

<?php

    //You don't need that
    /*if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }*/

    //Try this:
    <?php
          wp_reset_query();
          $paged = get_query_var('paged', 1);

           $args = array(
            'post_type' => $post_type,
            'posts_per_page' => '12',
            'paged' => $paged,
            'order' => 'DESC',
            'orderby' => 'post-date'
          );
          $loop = new WP_Query( $args);
          ?>
相关问题