0

I would like to build on this answer so I can incorporate pagination and a couple of other features.

We have over 1,600 entries in our Glossary so certain letters have far more than 20 (my max posts/page) entries.

If a user clicks "A" or accesses "/dev/glossary/a" I'd like them to see a list of all entries with parent "A".

I'd also like:

  • The list to be in alphabetical order
  • The number of entries for this parent to be displayed at the top of the page
  • The list to be paginated

I've had a basic attempt at this using $paged = (get_query_var('page')) ? get_query_var('page') : 1; and the paged argument in the WP_Query, but my next_posts_link doesn't even display.

I'm not quite sure where to begin with this so any guidance would be much appreciated.

  • Just to clarify, if you're listing all entries, where would the pagination come in? – CookiesForDevo Mar 17 '12 at 14:51
  • Sorry, what I mean is, I'd like the user to be able to see 20 entries at a time, but I'd like all of the entries to be accessible via the pagination (i.e. click "next page" a few times and the third lot of 20 glossary entries under "A" would be displayed). – dunc Mar 17 '12 at 17:34
1

Something like this should do the trick for ordering by title, and using pagination:

    global $wp_rewrite, $wp_query;
    if ( have_posts() ) : while ( have_posts() ) : the_post();  ?>

    <header class="entry-header">
        <h1 class="entry-title"><?php the_title(); ?></h1>
    </header><!-- .entry-header -->

    <?php   //If top level, find children
        if($post->post_parent == 0):
            $children = new WP_Query(array(
                'post_type'=>'page',
                'post_parent'=>$post->ID,
                'paged' => $paged,
                'posts_per_page' => 20,
                'orderby' => 'title',
                'order' => ASC
            ));

            if($children->have_posts()): ?>
                <ul>
                <?php while ( $children->have_posts() ) : $children->the_post(); ?>
                    <li> <?php the_title();?> </li>
                <?php endwhile; ?>
                </ul>

                $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

                $pagination = array(
                    'base' => @add_query_arg('paged','%#%'),
                    'total' => $wp_query->max_num_pages,
                    'current' => $current
                );

                if( $wp_rewrite->using_permalinks() )
                    $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );

                if( !empty($wp_query->query_vars['s']) )
                    $pagination['add_args'] = array( 's' => get_query_var( 's' ) ); ?>

                <?php echo paginate_links( $pagination ); ?>

            <?php else: ?>
                <?php echo 'No children';?>
            <?php endif; ?>

            <?php wp_reset_postdata(); ?>
        <?php else://Not top level, display normal post ?>
            <?php the_content(); ?>
        <?php endif;?>

    <?php endwhile; // end of the loop. ?>
<?php endif; ?>

You can move the paginate_links function to wherever you want the pagination to go.

As for displaying the total number of entries, you can use the $wp_query variable inside of the loop.

<?php echo $wp_query->found_posts; ?>

0

i wrote about creating a custom 'glossary' taxonomy from post titles. it isn't perfect, but it might be relevant:

http://www.kathyisawesome.com/424/alphabetical-posts-glossary/

it is automatic, but there is not reason you couldn't add a metabox, if the auto-generated letters aren't working quite well enough.

from there i'd probably use the pre_get_posts() hook to force 20 posts/page and alphabetical orderby (kind of can't believe i didn't already have the orderby... should probably update my tutorial at some point.

0

Thanks to @DaveHunt's answer as well as this excellent answer, I've been able to put the following code together.

It's important to note that because the page is_single(), the paged variable will always be set to 1. I spent a lot of time on this because of that oversight on my part!

if($post->post_parent == 0):

    if ( get_query_var( 'tpage' ) > 0 )
        $paged = get_query_var( 'tpage' );
    else
        $paged = 1;

    $base = trailingslashit( get_permalink( $post->ID ) );

    $children = new WP_Query( array(
        'post_type'=>'glossary',
        'post_parent'=>$post->ID,
        'paged' => $paged,
        'posts_per_page' => 20,
        'orderby' => 'title',
        'order' => ASC
    ) );

    if ( $children->have_posts() ) : ?>

        <?php while ( $children->have_posts() ) : $children->the_post(); ?>
            <?php get_template_part( 'list', 'glossary' ); ?>
        <?php endwhile; ?>

        <?php

            $links = array(
                'base' => $base . 'tpage/%#%',
                'total' => $children->max_num_pages,
                'current' => $paged
            );

        ?>

        <div class="navigation">
            <?php echo paginate_links( $links ); ?>
        </div>

    <?php else: ?>
        Sorry, there are no entries in our glossary for this letter.
    <?php endif; ?>

    <?php wp_reset_postdata(); ?>

<?php else : ?>

Hope this helps any other users coming across a similar problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.