PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, March 13, 2022

[FIXED] How to add pagination to my custom page template

 March 13, 2022     pagination, wordpress     No comments   

Issue

I just created a new custom page template and called two posts only. How can I add pagination so that I can the link with at least two of the lastest posts?

I tried this code but it does not work:

<?php
    $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
    query_posts( 
        array ( 
            'post_type' => 'post', 
            'category_name' => 'news', 
            'category' => 1,
            'posts_per_page' => 2,
            'paged' => $paged ) 
        );      
        // The Loop
        while ( have_posts() ) : the_post();?>
            <div class="news-page-content-wrapper">
                <div class="news-page-content">
                    <h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
                    <figure><?php the_post_thumbnail(); ?></figure>
                    <p><?php echo get_the_excerpt();?></p>
                    <a href="<?php the_permalink(); ?>">Read More&raquo</a>
                </div>
            </div>   
        <?endwhile; 
        // Reset Query
        wp_reset_query();
    ?>

Any help?


Solution

Since you're using "the loop" you should use the built in function for displaying pagination.

Here's some examples for you: https://codex.wordpress.org/Pagination

I've updated your sample code to show the default pagination:

<?php
    $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
    query_posts( 
        array ( 
            'post_type' => 'post', 
            'category_name' => 'news', 
            'posts_per_page' => 2,
            'paged' => $paged ) 
        );      
        // The Loop
        while ( have_posts() ) : the_post(); ?>
            <div class="news-page-content-wrapper">
                <div class="news-page-content">
                    <h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                    <figure><?php the_post_thumbnail(); ?></figure>
                    <p><?php echo get_the_excerpt(); ?></p>
                    <a href="<?php the_permalink(); ?>">Read More&raquo; </a>
                </div>
            </div>   
        <?php endwhile; 

        the_post_navigation();
        // Reset Query
        wp_reset_query();
    ?>


Answered By - Daniel
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing