自定義循環中的分頁無法正常工作

[英]Pagination in custom loop not working


I have already searched several topics and questions regarding this problem, but I haven´t found any answer that suits my code.

我已經搜索了有關此問題的幾個主題和問題,但我找不到任何適合我的代碼的答案。

The pagination for my custom posts is displayed, but when I click in Show next posts >> the same posts from the previous page are shown, even though the URL shows ?paged=2.

顯示我的自定義帖子的分頁,但是當我單擊顯示下一個帖子>>時,顯示上一頁中的相同帖子,即使URL顯示?paged = 2。

My code is the following:

我的代碼如下:

<div class="podcast-entries">
    <?php
    $args = array( 'post_type' => 'strn5_podcasts',
                   'posts_per_page' => 3,
                   'paged' => (get_query_var('paged') ? get_query_var('paged') : 1 );
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( $args );

    if ( $wp_query->have_posts() ) : 
    while ( $wp_query->have_posts() ) :
    $wp_query->the_post();
    ?>

        <div <?php post_class(); ?> >

            <h4 class="podcast-title"><?php the_title(); ?></h4>

            <div class="podcast-content">
                <?php the_content(); ?>
            </div>

            <h6><?php the_time('F j, Y'); ?></h6>

            <div class="post-separator col-lg-12"></div>

        </div>

    <?php endwhile; endif; ?>

    <div class="navigation-links">

        <div class="next-post">
            <?php next_posts_link( '>> Siguiente Entrada' ); ?>
        </div>

        <div class="previous-post">
            <?php previous_posts_link( 'Anterior Entrada >>' ); ?>
        </div>

    </div>

    <?php $wp_query = null; 
    $wp_query = $temp;
    wp_reset_query(); ?>

</div> <!-- .podcast-entries -->

2 个解决方案

#1


0  

Usually what I do when something doesn't work is that I strip it from everything and start rebuilding it with bare basics to see what the problem might be.

通常我做的事情不起作用的是我將它從一切中刪除並開始用基本的基礎重建它以查看問題可能是什么。

Why don't you try a simpler form of loop to see if you can get it to prin.t

你為什么不嘗試一種更簡單的循環形式來看看你是否可以把它變成prin.t.

<?php query_posts(array('posts_per_page' => 3, 'post_type' => 'strn5_podcasts', 'paged' => get_query_var('page'))); if (have_posts()) : while (have_posts()) : the_post(); ?>
  <p><?php the_title(); ?></p>
<?php endwhile; endif;?>

<div class="navigation-links">
  <div class="next-post"><?php next_posts_link( '>> Siguiente Entrada' ); ?></div>
  <div class="previous-post"><?php previous_posts_link( 'Anterior Entrada >>' ); ?></div>
</div>

It's pretty much the same thing you are doing (almost) but without any variables or any other thing that "could" go wrong.

這幾乎和你做的差不多(差不多)但沒有任何變量或任何“可能”出錯的東西。

Edit

In the end it turned out that the paged parameter can be used with paged or page and in this case it worked out for you with page instead of paged

最后,結果是paged參數可以與分頁或頁面一起使用,在這種情況下,它可以為您提供頁面而不是分頁

So it ended up looking like so:

所以最終看起來像這樣:

'paged' => get_query_var('page')

'paged'=> get_query_var('page')

On a final note, using get_query_var('page') is needed if you're using query posts in a custom page template that you've set as your homepage.

最后,如果您在自定義頁面模板中使用查詢帖子,則需要使用get_query_var('page')作為主頁。

#2


0  

I agree with sulfureous. I think more specifically though, these three lines are the problem:

我同意硫磺。我更具體地說,這三行是問題所在:

$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );

Based off of your description it seems like this would cause the issue if $wp_query is being caught as not null. I'm not sure why you'd even have that in there but you must have your reason.

根據您的描述,如果$ wp_query被捕獲為非null,這似乎會導致問題。我不確定你為什么要在那里擁有它,但你必須有你的理由。

At the very least take it out the first two lines of that to help you troubleshoot.

至少要把它排除在前兩行,以幫助您排除故障。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/08/10/e73580b848a9cfbc4b4ddd8ceea94fbe.html



 
  © 2014-2022 ITdaan.com