【问题标题】:Wordpress pagination, wp_query not working on page 2Wordpress 分页,wp_query 在第 2 页上不起作用
【发布时间】:2015-10-01 12:11:41
【问题描述】:

我在使用 WP 分页时遇到问题。

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

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

没有文件被编辑,功能和3天前一样。

PS:如果我删除第一个bug的更正(第2页显示与第1页相同),bug不会回来,首页第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>

感谢您的帮助,我到处搜索,但发现了有关此问题的任何信息。

【问题讨论】:

    标签: php loops pagination wordpress


    【解决方案1】:

    你的条件太多了。

    <?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);
              ?>
    

    【讨论】:

    • 我解决了这个问题: $queryPaged = get_query_var('paged'); if ( $queryPaged) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } 其他 { $paged = 1; } $分页; if ($search) { $args = array( 'posts_per_page' => 12, 'paged'=>$paged, 'orderby'=> 'date' ); $args['s'] = $搜索; }else{ $args = array( 'post_type' => $post_type, 'posts_per_page' => 12, 'orderby'=> 'date', 'tax_query' => array(array($taxQuery)), 'paged'= > $分页); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2015-12-31
    相关资源
    最近更新 更多