user1543784 user1543784 - 6 months ago 38
PHP Question

WordPress pagination links not showing up for custom post type query

I am trying to paginate list of custom post types. This list shows okay, but I can't seem to make pagination links to show.

Here is the code:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
'post_type' => 'families',
'posts_per_page' => 9,
'paged'=>$paged
);

$the_query = new WP_Query( $args );

?>

<table class="families">

<tr>
<th class="family_head">Family Head</th>
<th class="address">Address</th>
<th class="contact_number">Contact number</th>
</tr>

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<tr>

<td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>

<td><?php the_field( 'residence_address' ); ?></td>

<td class="contact_number"><?php the_field( 'mobile_number_1' ); ?></td>

</tr>

<?php endwhile; ?>

<div class="navigation">
<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
<div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>

<?php else: ?>

<?php endif; ?>

</table>


Other similar WP functions for pagination aren't working either. What am I doing wrong?

Answer

I erased all the code for pagination and re-did it. It works now. See if you can find the change.

Here is the working code -

<div id="main_content">

<div id="main_content_otherpages">

    <?php

        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

        $args = array(
            'post_type' => 'families',
            'posts_per_page' => 20,
            'paged' => $paged
        );

        $the_query = new WP_Query( $args );

    ?>

    <table class="families">

        <tr>
            <th class="family_head">Family Head</th>
            <th class="address">Address</th>
            <th class="contact_number">Contact number</th>
        </tr>

        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <tr>

            <td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>  

            <td><?php the_field( 'residence_address' ); ?></td>

            <td class="contact_number"><?php the_field( 'mobile_number_1' ); ?></td>

        </tr>

        <?php endwhile; ?>

    </table>

    <div style="clear:none; float:left"><?php next_posts_link( '&larr;Previous', $the_query->max_num_pages ); ?></div>
    <div style="clear:none; float:right"><?php previous_posts_link( 'Next&rarr;' ); ?></div>

    <?php wp_reset_postdata(); ?>

    <?php else: ?>

    <?php endif; ?>

</div>