0

In Wordpress how do you get pagination to render on a custom page template?

https://developer.wordpress.org/themes/functionality/pagination/

Examples #Examples
Loop with Pagination #Loop with Pagination
This simplified example shows where you can add pagination functions for the main loop. Add the functions just before or after the loop.

<?php if ( have_posts() ) : ?>

&lt;!-- Add the pagination functions here. --&gt;

&lt;!-- Start of the main loop. --&gt;
&lt;?php while ( have_posts() ) : the_post(); ?&gt;

&lt;!-- the rest of your theme's main loop --&gt;

&lt;?php endwhile; ?&gt;
&lt;!-- End of the main loop --&gt;

&lt;!-- Add the pagination functions here. --&gt;


<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>

<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

<?php else : ?>

<?php _e('Sorry, no posts matched your criteria.'); ?>

<?php endif; ?> Expand full source code

So here is my code block:

// this variable referenced represents 16
<?php $posts_to_display = 16; ?>

<section class="flo-section resources-blocks"> <div class="grid-container"> <div class="grid-x grid-margin-x align-center" id="container"> <?php $args = array( 'post_type' => $resourcesPostTypes, 'post_status' => array('publish'), 'orderby' => 'date', 'order' => 'DESC', 'post__not_in' => array(15552, 15554, 15555), 'posts_per_page' => $posts_to_display );

        $query = new WP_Query($args);
        $resourceCount = 0;

        if ($query-&gt;have_posts()) {
            while ($query-&gt;have_posts()) {
                $query-&gt;the_post();
                $resourceCount++;

                $post_type = get_post_type();
                $post_type_object = get_post_type_object($post_type);
                if ($post_type_object) {
                    $post_type_display = esc_html($post_type_object-&gt;labels-&gt;singular_name);
                }
                ?&gt;
                &lt;div class=&quot;cell small-12 medium-4 large-3&quot;&gt;
                    &lt;?php
                    $is_downloadable = get_field('is_downloadable');
                    $asset_redirect = get_field('asset_redirect');
                    if ($is_downloadable) {
                        $download_link = get_field('download_link');
                        $resource_url = $download_link['url'];
                    } else if ($asset_redirect) {
                        $resource_url = $asset_redirect;
                    } 
                    else {
                        $resource_url = get_permalink();
                    }
                    ?&gt;
                    &lt;a class=&quot;asset-block &lt;?php echo ($post_type); ?&gt;&quot; href=&quot;&lt;?php echo $resource_url; ?&gt;&quot;&gt;
                        &lt;i class=&quot;icon-&lt;?php echo $post_type; ?&gt;&quot;&gt;&lt;/i&gt;
                        &lt;p class=&quot;content-type&quot;&gt;&lt;?php echo ucfirst($post_type_display); ?&gt;&lt;/p&gt;
                        &lt;h4&gt;&lt;?php echo wp_trim_words(get_the_title(), 20); ?&gt;&lt;/h4&gt;
                    &lt;/a&gt;
                &lt;/div&gt;
                &lt;?php
            }
        } else {
            // no posts found
        }

        wp_reset_postdata();
        ?&gt;
    &lt;/div&gt;
    &lt;?php
    $args = array(
        'post_type' =&gt; $resourcesPostTypes,
        'orderby' =&gt; 'date',
        'order' =&gt; 'DESC',
        'posts_per_page' =&gt; -1,
        'post__not_in'   =&gt; array(15552, 15554, 15555)
    );

    $query = new WP_Query($args);
    if (!empty($query) &amp;&amp; $query-&gt;post_count &gt; $posts_to_display) {
        $display_load_more = &quot;display:block;&quot;;
    } else {
        $display_load_more = &quot;display:none;&quot;;
    }
    ?&gt;
    &lt;div style=&quot;color: red;&quot;&gt;&lt;?php echo $display_load_more; ?&gt;&lt;/div&gt;
    &lt;div class=&quot;text-center&quot; id=&quot;load-more&quot; style=&quot;&lt;?php echo $display_load_more; ?&gt;&quot;&gt;
        // HERE IS THE PAGINATION
        &lt;?php the_posts_pagination(); ?&gt;
    &lt;/div&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;next_page&quot; id=&quot;next_page&quot; value=&quot;2&quot; /&gt;
    &lt;div class=&quot;hide&quot; id=&quot;resources-loader&quot;&gt;
        &lt;img src=&quot;&lt;?= get_template_directory_uri(); ?&gt;/assets/images/ajax-loader.gif&quot; /&gt;
    &lt;/div&gt;
&lt;/div&gt;

</section>

I thought maybe it wasnt close enough to the loop like the documentation said so I brought it closer

<section class="flo-section resources-blocks">
    <div class="grid-container">
        <div class="grid-x grid-margin-x align-center" id="container">
            <?php
            $args = array(
                'post_type' => $resourcesPostTypes,
                'post_status' => array('publish'),
                'orderby' => 'date',
                'order' => 'DESC',
                'post__not_in'   => array(15552, 15554, 15555),
                'posts_per_page' => $posts_to_display
            );
        $query = new WP_Query($args);
        $resourceCount = 0;

        if ($query-&gt;have_posts()) {
            while ($query-&gt;have_posts()) {
                $query-&gt;the_post();
                $resourceCount++;

                $post_type = get_post_type();
                $post_type_object = get_post_type_object($post_type);
                if ($post_type_object) {
                    $post_type_display = esc_html($post_type_object-&gt;labels-&gt;singular_name);
                }
                ?&gt;
                &lt;div class=&quot;cell small-12 medium-4 large-3&quot;&gt;
                    &lt;?php
                    $is_downloadable = get_field('is_downloadable');
                    $asset_redirect = get_field('asset_redirect');
                    if ($is_downloadable) {
                        $download_link = get_field('download_link');
                        $resource_url = $download_link['url'];
                    } else if ($asset_redirect) {
                        $resource_url = $asset_redirect;
                    } 
                    else {
                        $resource_url = get_permalink();
                    }
                    ?&gt;
                    &lt;a class=&quot;asset-block &lt;?php echo ($post_type); ?&gt;&quot; href=&quot;&lt;?php echo $resource_url; ?&gt;&quot;&gt;
                        &lt;i class=&quot;icon-&lt;?php echo $post_type; ?&gt;&quot;&gt;&lt;/i&gt;
                        &lt;p class=&quot;content-type&quot;&gt;&lt;?php echo ucfirst($post_type_display); ?&gt;&lt;/p&gt;
                        &lt;h4&gt;&lt;?php echo wp_trim_words(get_the_title(), 20); ?&gt;&lt;/h4&gt;
                    &lt;/a&gt;
                &lt;/div&gt;
                &lt;?php
            }
            // HELLOOOOO PAGINATION WHERE ARE YOU????
            the_posts_pagination();
        } else {
            // no posts found
        }

        wp_reset_postdata();
        ?&gt;
    &lt;/div&gt;
    &lt;?php
    $args = array(
        'post_type' =&gt; $resourcesPostTypes,
        'orderby' =&gt; 'date',
        'order' =&gt; 'DESC',
        'posts_per_page' =&gt; -1,
        'post__not_in'   =&gt; array(15552, 15554, 15555)
    );

    $query = new WP_Query($args);
    if (!empty($query) &amp;&amp; $query-&gt;post_count &gt; $posts_to_display) {
        $display_load_more = &quot;display:block;&quot;;
    } else {
        $display_load_more = &quot;display:none;&quot;;
    }
    ?&gt;
    &lt;div style=&quot;color: red;&quot;&gt;&lt;?php echo $display_load_more; ?&gt;&lt;/div&gt;
    &lt;div class=&quot;text-center&quot; id=&quot;load-more&quot; style=&quot;&lt;?php echo $display_load_more; ?&gt;&quot;&gt;
    &lt;/div&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;next_page&quot; id=&quot;next_page&quot; value=&quot;2&quot; /&gt;
    &lt;div class=&quot;hide&quot; id=&quot;resources-loader&quot;&gt;
        &lt;img src=&quot;&lt;?= get_template_directory_uri(); ?&gt;/assets/images/ajax-loader.gif&quot; /&gt;
    &lt;/div&gt;
&lt;/div&gt;

</section>

And then I realized:

https://stackoverflow.com/questions/30066849/why-the-posts-pagination-function-not-working

the_posts_pagination only works in post listings pages like index.php or archive.php. It will not work in a custom template, for example.

See here : https://codex.wordpress.org/Function_Reference/the_posts_pagination

Okay, so let's try posts_nav_link()

https://developer.wordpress.org/reference/functions/posts_nav_link/

Displays the post pages link navigation for previous and next pages.

<section class="flo-section resources-blocks">
    <div class="grid-container">
        <div class="grid-x grid-margin-x align-center" id="container">
            <?php
            $args = array(
                'post_type' => $resourcesPostTypes,
                'post_status' => array('publish'),
                'orderby' => 'date',
                'order' => 'DESC',
                'post__not_in'   => array(15552, 15554, 15555),
                'posts_per_page' => $posts_to_display
            );
        $query = new WP_Query($args);
        $resourceCount = 0;

        if ($query-&gt;have_posts()) {
            while ($query-&gt;have_posts()) {
                $query-&gt;the_post();
                $resourceCount++;

                $post_type = get_post_type();
                $post_type_object = get_post_type_object($post_type);
                if ($post_type_object) {
                    $post_type_display = esc_html($post_type_object-&gt;labels-&gt;singular_name);
                }
                ?&gt;
                &lt;div class=&quot;cell small-12 medium-4 large-3&quot;&gt;
                    &lt;?php
                    $is_downloadable = get_field('is_downloadable');
                    $asset_redirect = get_field('asset_redirect');
                    if ($is_downloadable) {
                        $download_link = get_field('download_link');
                        $resource_url = $download_link['url'];
                    } else if ($asset_redirect) {
                        $resource_url = $asset_redirect;
                    } 
                    else {
                        $resource_url = get_permalink();
                    }
                    ?&gt;
                    &lt;a class=&quot;asset-block &lt;?php echo ($post_type); ?&gt;&quot; href=&quot;&lt;?php echo $resource_url; ?&gt;&quot;&gt;
                        &lt;i class=&quot;icon-&lt;?php echo $post_type; ?&gt;&quot;&gt;&lt;/i&gt;
                        &lt;p class=&quot;content-type&quot;&gt;&lt;?php echo ucfirst($post_type_display); ?&gt;&lt;/p&gt;
                        &lt;h4&gt;&lt;?php echo wp_trim_words(get_the_title(), 20); ?&gt;&lt;/h4&gt;
                    &lt;/a&gt;
                &lt;/div&gt;
                &lt;?php
            }
        } else {
            // no posts found
        }

        wp_reset_postdata();
        ?&gt;
    &lt;/div&gt;
    &lt;?php
    $args = array(
        'post_type' =&gt; $resourcesPostTypes,
        'orderby' =&gt; 'date',
        'order' =&gt; 'DESC',
        'posts_per_page' =&gt; -1,
        'post__not_in'   =&gt; array(15552, 15554, 15555)
    );

    $query = new WP_Query($args);
    if (!empty($query) &amp;&amp; $query-&gt;post_count &gt; $posts_to_display) {
        $display_load_more = &quot;display:block;&quot;;
    } else {
        $display_load_more = &quot;display:none;&quot;;
    }
    ?&gt;
    &lt;div style=&quot;color: red;&quot;&gt;&lt;?php echo $display_load_more; ?&gt;&lt;/div&gt;
    &lt;div class=&quot;text-center&quot; id=&quot;load-more&quot; style=&quot;&lt;?php echo $display_load_more; ?&gt;&quot;&gt;
        &lt;?php posts_nav_link(); ?&gt;
    &lt;/div&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;next_page&quot; id=&quot;next_page&quot; value=&quot;2&quot; /&gt;
    &lt;div class=&quot;hide&quot; id=&quot;resources-loader&quot;&gt;
        &lt;img src=&quot;&lt;?= get_template_directory_uri(); ?&gt;/assets/images/ajax-loader.gif&quot; /&gt;
    &lt;/div&gt;
&lt;/div&gt;

</section>

Still nothing....

enter image description here

How do you get Wordpress to render pagination on a custom page?

MORE EDITS:

https://stackoverflow.com/questions/42480019/showing-prev-next-post-links-doesnt-work

In Layman language it is a wordpress function to fetch posts/custom-posts. Internally it calls WP_Query function. Or you can use WP_Query instead of this. 'posts_nav_link' function does not work with WP_Query & get_posts function. You can try this to get next previous links.

Got it, ok, how do we paginate with WP_Query? Let's see

https://developer.wordpress.org/reference/functions/paginate_links/

Looking promising, can we work it with WP_Query() ?

Pagination when using wp_query?

WordPress comes with a handy function called paginate_links() which does the heavy lifting. In the example above, the custom WP_Query object $query is used instead of the global $wp_query object.

Looking good ok lets try it:

<section class="flo-section resources-blocks">
    <div class="grid-container">
        <div class="grid-x grid-margin-x align-center" id="container">
            <?php
            $args = array(
                'post_type' => $resourcesPostTypes,
                'post_status' => array('publish'),
                'orderby' => 'date',
                'order' => 'DESC',
                'post__not_in'   => array(15552, 15554, 15555),
                'posts_per_page' => $posts_to_display
            );
        $query = new WP_Query($args);
        $resourceCount = 0;

        if ($query-&gt;have_posts()) {
            while ($query-&gt;have_posts()) {
                $query-&gt;the_post();
                $resourceCount++;

                $post_type = get_post_type();
                $post_type_object = get_post_type_object($post_type);
                if ($post_type_object) {
                    $post_type_display = esc_html($post_type_object-&gt;labels-&gt;singular_name);
                }
                ?&gt;
                &lt;div class=&quot;cell small-12 medium-4 large-3&quot;&gt;
                    &lt;?php
                    $is_downloadable = get_field('is_downloadable');
                    $asset_redirect = get_field('asset_redirect');
                    if ($is_downloadable) {
                        $download_link = get_field('download_link');
                        $resource_url = $download_link['url'];
                    } else if ($asset_redirect) {
                        $resource_url = $asset_redirect;
                    } 
                    else {
                        $resource_url = get_permalink();
                    }
                    ?&gt;
                    &lt;a class=&quot;asset-block &lt;?php echo ($post_type); ?&gt;&quot; href=&quot;&lt;?php echo $resource_url; ?&gt;&quot;&gt;
                        &lt;i class=&quot;icon-&lt;?php echo $post_type; ?&gt;&quot;&gt;&lt;/i&gt;
                        &lt;p class=&quot;content-type&quot;&gt;&lt;?php echo ucfirst($post_type_display); ?&gt;&lt;/p&gt;
                        &lt;h4&gt;&lt;?php echo wp_trim_words(get_the_title(), 20); ?&gt;&lt;/h4&gt;
                    &lt;/a&gt;
                &lt;/div&gt;
                &lt;?php
            }
            paginate_links();
        } else {
            // no posts found
        }

        wp_reset_postdata();
        ?&gt;
    &lt;/div&gt;
    &lt;?php
    $args = array(
        'post_type' =&gt; $resourcesPostTypes,
        'orderby' =&gt; 'date',
        'order' =&gt; 'DESC',
        'posts_per_page' =&gt; -1,
        'post__not_in'   =&gt; array(15552, 15554, 15555)
    );

    $query = new WP_Query($args);
    if (!empty($query) &amp;&amp; $query-&gt;post_count &gt; $posts_to_display) {
        $display_load_more = &quot;display:block;&quot;;
    } else {
        $display_load_more = &quot;display:none;&quot;;
    }
    ?&gt;
    &lt;div style=&quot;color: red;&quot;&gt;&lt;?php echo $display_load_more; ?&gt;&lt;/div&gt;
    &lt;div class=&quot;text-center&quot; id=&quot;load-more&quot; style=&quot;&lt;?php echo $display_load_more; ?&gt;&quot;&gt;
        &lt;?php paginate_links(); ?&gt;
    &lt;/div&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;next_page&quot; id=&quot;next_page&quot; value=&quot;2&quot; /&gt;
    &lt;div class=&quot;hide&quot; id=&quot;resources-loader&quot;&gt;
        &lt;img src=&quot;&lt;?= get_template_directory_uri(); ?&gt;/assets/images/ajax-loader.gif&quot; /&gt;
    &lt;/div&gt;
&lt;/div&gt;

</section>

Still nothing.

Tom J Nowell
  • 61,323
  • 7
  • 79
  • 150
kawnah
  • 123
  • 1
  • 7

0 Answers0