简体   繁体   中英

Wordpress Pagination wp_query

I have a custom page which lists ALL of the posts no matter the category, I have hit a brick wall with the pagination! for some reason the pagination ins't showing up!

Here is my code

<?php // Template Name: News Feed ?>
<?php get_header(); ?>
<div id="content">
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <div id="left-sidebar">
                    <?php get_sidebar('posts') ?>
                </div>
            </div>
            <div class="col-md-6">
            <?php
                $args = array(
                    'post_type' => 'post',
                    'orderby'   => 'date',
                    'order'     => 'DESC',
                );
                $my_query = new WP_Query($args);

                    while ($my_query->have_posts()) : $my_query->the_post(); ?>

                        <div class="post">
                            <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                            <small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
                            <div class="entry">
                                <?php the_excerpt(); ?>
                            </div>
                            <p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
                        </div> 

                    <?php endwhile; ?>

                    <div class="navigation">
                        <div class="alignleft"><?php previous_posts_link('Previous Entries'); ?></div>
                        <div class="alignright"><?php next_posts_link('Next Entries'); ?></div>
                    </div>

            </div>
            <div class="col-md-3">
                <?php get_sidebar(); ?>
            </div>
        </div>
    </div>
</div>
<?php get_footer(); ?>

Your links will not show up because next_posts_link() is set to the $max_num_pages parameter of the main query ( $wp_query->max_num_pages ) by default. On pages, this will always be 1 and by default these links don't display when there is only one page

Also, you are not paginating your query, so even if you gt your links working, you will see the same posts being repeated.

You should add the paged parameter to your query like this

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'paged' => $paged,
    // Rest of you arguments
);

and then you need to alter the $max_pages parameter in next_posts_link() like this

next_posts_link( 'Next entries', $my_query->max_num_pages );

First try to add 'posts_per_page' parameter into the $args, so the loop will know how much posts to display in the page.

Second,From WordPress Codex :

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

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

query_posts('posts_per_page=3&paged=' . $paged); 
?>

That means Just add this code before your args (without the "query_posts") and then but the args like this :

$args = array(
                    'post_type' => 'post',
                    'paged'     => $paged,
                    'orderby'   => 'date',
                    'order'     => 'DESC',

I am recommending to explore the codex once again, maybe there is something there that can help you. http://codex.wordpress.org/Pagination#Example_Loop_with_Pagination

You have created custom template so you should try with the following link first: https://wordpress.org/support/topic/pagination-not-working-on-custom-template-1

Also, I request you to go through this link and it explain everything in details

By default, in any given context, WordPress uses the main query to determine pagination. The main query object is stored in the $wp_query global, which is also used to output the main query loop:

 if ( have_posts() ) : while ( have_posts() ) : the_post();

When you use a custom query, you create an entirely separate query object:

 $custom_query = new WP_Query( $custom_query_args );

And that query is output via an entirely separate loop:

if ( $custom_query->have_posts() ) : 
while ( $custom_query->have_posts() ) : 
    $custom_query->the_post();

But pagination template tags, including previous_posts_link(), next_posts_link(), posts_nav_link(), and paginate_links(), base their output on the main query object, $wp_query. That main query may or may not be paginated. If the current context is a custom page template, for example, the main $wp_query object will consist of only a single post - that of the ID of the page to which the custom page template is assigned.

If the current context is an archive index of some sort, the main $wp_query may consist of enough posts to cause pagination, which leads to the next part of the problem: for the main $wp_query object, WordPress will pass a paged parameter to the query, based on the paged URL query variable. When the query is fetched, that paged parameter will be used to determine which set of paginated posts to return. If a displayed pagination link is clicked, and the next page loaded, your custom query won't have any way to know that the pagination has changed.

https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops

I hope this will work for you

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination

    $the_query = new WP_Query( array( 
        'post_type' => 'post',
        'paged' => $paged,
        'posts_per_page' => 5) 
    );

    while ( $the_query->have_posts() ) : $the_query->the_post();
        echo '<div>' . get_the_title() . '</div>';
              the_content();
    endwhile;


    echo '<nav>';
    echo  '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
    echo  '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
    echo "</nav>";


    wp_reset_postdata(); // Rest Data

Would you please try above code?

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