简体   繁体   中英

Wordpress pagination not working in custom query

I have set my front page by using front-page.php , and am displaying my latest bbpress topics,in first section. i want to display only 5 posts, and pagination.But pagination is not working. I checked on inspect element,class is coming but no links are present. Following is my code

<ul>
                                <?php 
                                    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                                    $args = array( 'post_type' => 'topic', 'posts_per_page' => 2 , 'paged' => $paged);
                                    $loop = new WP_Query( $args );
                                   if($loop->have_posts()):
                                    while($loop->have_posts()):
                                        $loop->the_post();
                                    $topic = get_the_ID();
                                    ?>
                                    <li>
                                        <div class="bbp_topic_data">
                                            <div class="bbp_topic_post">
                                                <a href="<?php bbp_topic_permalink() ?>">
                                                    <h4><?= the_title(); ?></h4>
                                                    <?=the_excerpt();?>
                                                </a>
                                            </div>
                                            <div class="bbp_topic_stat">
                                                <!-- <span><?php //do_action( 'bbp_theme_after_topic_started_by' ); ?></span> -->
                                                <span class="topic_details_text"><?php echo $view_count=  pvc_post_views();; if($view_count <  0) {echo $view_count;}?></span>
                                                <?php $reply_count=  bbp_get_topic_reply_count(  $topic_id = $topic ); ?>
                                                <span class="topic_details_text"><?php if($reply_count > 0)  {echo "Replies:". $reply_count;} ?> </span>
                                            </div>
                                            
                                        </div><!--bbp_topic_data-->
                                        <div>
                                            <span class="author_image"><?php echo bbp_get_topic_author_avatar($topic)?></span>
                                            <span class="topic_details_text">Started By: <?php echo bbp_get_topic_author_display_name( $topic ); ?> </span>
                                            
                                        </div>
                                    </li>
                                   <?php endwhile; ?>
                                   <div class="next-prev-wrap">

<span class="next"><?php next_posts_link( 'Older posts', $post->max_num_pages ); ?></span>
<span class="prev"><?php previous_posts_link( 'Newer posts', $post->max_num_pages ); ?></span>

</div>
                                    <?php endif; ?>
                                   <?php next_posts_link(); ?>
                                   <?php //wp_reset_postdata(); ?>
                            </ul>

Please help me in solving this

The problem is in the $post variable you need replace to $loop and you should check the paged parameter

If the pagination is broken on a static front page you have to add the "paged" parameter this way:

 if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); } elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); } else { $paged = 1; }

Check here, please - https://codex.wordpress.org/Pagination

<ul>
    
    <?php
    if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
    elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
    else { $paged = 1; }
    $args = array( 'post_type' => 'topic', 'posts_per_page' => 2 , 'paged' => $paged);
    $loop = new WP_Query( $args );
    if( $loop->have_posts()):
        while($loop->have_posts()):
            $loop->the_post();
            $topic = get_the_ID();
            ?>
            <li>
                <div class="bbp_topic_data">
                    <div class="bbp_topic_post">
                        <a href="<?php the_permalink() ?>">
                            <h4><?= the_title(); ?></h4>
                            <?php the_excerpt();?>
                        </a>
                    </div>
                    <div class="bbp_topic_stat">
                         <span><?php do_action( 'bbp_theme_after_topic_started_by' ); ?></span>
                        <span class="topic_details_text"><?php echo $view_count=  pvc_post_views();; if($view_count <  0) {echo $view_count;}?></span>
                        <?php $reply_count=  bbp_get_topic_reply_count(  $topic_id = $topic ); ?>
                        <span class="topic_details_text"><?php if($reply_count > 0)  {echo "Replies:". $reply_count;} ?> </span>
                    </div>
    
                </div><!--bbp_topic_data-->
                <div>
                    <span class="author_image"><?php echo author_avatar($topic)?></span>
                    <span class="topic_details_text">Started By: <?php echo bbp_get_topic_author_display_name( $topic ); ?> </span>
    
                </div>
            </li>
        <?php endwhile; ?>
        <div class="next-prev-wrap">
    
            <div class="nav-previous alignleft"><?php previous_posts_link( 'Older posts' ); ?></div>
            <div class="nav-next alignright"><?php next_posts_link( 'Newer posts', $loop->max_num_pages ); ?></div>
    
    
        </div>
    <?php endif; ?>
    <?php next_posts_link(); ?>
    <?php wp_reset_postdata(); ?>
        </ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM