1

I am trying to achieve pagination on a custom template I have that displays on an archive of terms.

It appears to be working because when I navigate to mysite/taxonomy/page/2/ or 3,4,5 etc. the pagination works. It also shows the correct number of terms per page. However, there are not any actual pagination links showing on the bottom of my page and it sounds like there is supposed to be.

I am following this tutorial as well as the Codex instructions, but I have to be missing something. Can someone please help figure out what? My simplified code is below.

<?php get_header(); ?>

<div class="container">
<div class="row">
  <div class="col-md-12">

<div class="row">
  <div class="col-md-9">
<h2>Index Post Type Archive </h2>
<hr/>
    <!-- Begin LG Screen View-->
    <span class="visible-lg">
          <article> 

        <div class="row">
    <?php

    if ( get_query_var( 'paged' ) )
    $paged = get_query_var('paged');
else if ( get_query_var( 'page' ) )
    $paged = get_query_var( 'page' );
else
    $paged = 1;

$per_page    = 12;
$number_of_terms = count( get_terms( '100list' ) ); // This counts the total number terms in the taxonomy with a function)
$offset      = $per_page * ( $paged - 1) ;

    $libargs=array(
      'orderby'           => 'name',
         'order'             => 'ASC',
        'hide_empty'        => 0,
        'exclude'           => array(16, 20, 22,25, 27, 28, 30, 4, 42, 7, 43 ), //* Enter ID's of parent categories to exclude from list
        'taxonomy'      => '100list',
        'parent'               => '',
        'number'       => $per_page,
        'offset'       => $offset,

        );  

        $libcats=get_categories($libargs);  

        $i = 0;

        foreach($libcats as $lc){

            if( $i % 4 == 0 ) {
             echo '<div class="clearfix"></div>';
            }
          $i++;
            echo '<div class="col-lg-3">';

            $termlink = get_term_link( $lc->slug, '100list' );
        ?>
       <div class="thumbnail">
            <div class="caption">
                <br/><br/>
                <h1><span class="label label-warning"><?php echo  $lc->count  ?></span></h1>
                <p> Symbols </p>
                <p> <a class="label label-default" href="<?php echo $termlink; ?>"> View Group</a> </p>
            </div>
            <!-- Get Image by Attachment ID Start-->
            <?php
                $attachment_id = get_field('taximage', '100list_'.$lc->term_id);
                if ($attachment_id) {
                    $image = wp_get_attachment_image_src($attachment_id, 'industrygroup-img');
                    if ($image) {
                        ?>
                        <img class="img-responsive" src="<?php echo $image[0]; ?>" />
                        <?php     } }
            else { ?>
<img class="img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/08/RA_logo_300px_groups.jpg" alt="<?php the_title(); ?>" />

<?php } ?>
            <!-- Get Image by Attachment ID End-->
        </div>
    <small><p class="text-center"> <a href="<?php echo $termlink; ?>"> <?php echo $lc->name; ?></a> </p>  </small>

    <?php  echo '</div>'; } ?>

    <?php  if (function_exists("pagination")) {
$big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format'  => '/page/%#%',
        'current' => $paged,
        'total'   => ceil( $number_of_terms / $per_page ) // 20 items per page

    ) );
}
    ?>

</div>
</article>
</span>
      </div> <!-- End col-md-9 -->
    <div class="col-md-3">
      <?php include("sidebar100list.php"); ?>
 </div>  <!-- End col-md-3 -->
</div><!-- End row -->
</div><!-- End Container -->
<?php get_footer(); ?>

2 Answers 2

2

There where one or two problems with the code that I sorted out

Big changes

  • $number_of_terms = count( get_terms( '100list' ) ); is replaced by $number_of_terms = wp_count_terms( '100list' );. The reason is that wp_count_terms is already there to return the term count natively

  • get_categories is replaced by get_terms as get_terms accepts the offset parameter

  • Removed if (function_exists("pagination")) {. It serves no purpose, and it is the culprit that hides your pagination links.

  • Changed 'current' => $paged, to 'current' => max( 1, get_query_var('paged') ),

  • Cleaned up your indentation on the important part of your code. There seems to be closing </div>'s missing that you have to sort out.

So, here is the code that works. I did test it on my install and works perfectly

<?php get_header(); ?>

<div class="container">
<div class="row">
<div class="col-md-12">

    <div class="row">
        <div class="col-md-9">
            <h2>Index Post Type Archive </h2>
            <hr/>
            <!-- Begin LG Screen View-->
            <span class="visible-lg">
                <article> 

                    <div class="row">
                        <?php

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

                        $per_page = 4;
                        $number_of_terms = wp_count_terms( '100list' ); // This counts the total number terms in the taxonomy with a function)
                        $paged_offset = ($paged - 1) * $per_page;

                        $libargs = array(
                            'orderby'           => 'name',
                            'order'             => 'ASC',
                            'hide_empty'        => 0,
                            'exclude'           => array(16, 20, 22,25, 27, 28, 30, 4, 42, 7, 43 ), //* Enter ID's of parent categories to exclude from list
                            'number'            => $per_page,
                            'offset'            => $paged_offset
                        );  

                        $libcats = get_terms( '100list', $libargs);  

                        $i = 0;

                        foreach($libcats as $lc){
                            if( $i % 4 == 0 ) { ?>

                                <div class="clearfix"></div>

                            <?php }

                            $i++; ?>

                            <div class="col-lg-3">

                                <?php $termlink = get_term_link( $lc, '100list' ); ?>

                                <div class="thumbnail">
                                    <div class="caption">
                                        <br/><br/>
                                        <h1><span class="label label-warning"><?php echo  $lc->count  ?></span></h1>
                                        <p>Symbols</p>
                                        <p><a class="label label-default" href="<?php echo $termlink; ?>"> View Group</a></p>
                                    </div>

                                    <!-- Get Image by Attachment ID Start-->

                                    <?php $attachment_id = get_field('taximage', '100list_'.$lc->term_id);
                                    if ($attachment_id) {
                                        $image = wp_get_attachment_image_src($attachment_id, 'industrygroup-img');
                                        if ($image) { ?>

                                            <img class="img-responsive" src="<?php echo $image[0]; ?>" />

                                        <?php }   

                                    }else{ ?>
                                        <img class="img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/08/RA_logo_300px_groups.jpg" alt="<?php the_title(); ?>" />

                                    <?php } ?>

                                    <!-- Get Image by Attachment ID End-->
                                </div>      

                                    <small><p class="text-center"> <a href="<?php echo $termlink; ?>"> <?php echo $lc->name; ?></a></p></small>

                            </div>

                        <?php } 

                        $big = 999999999; // need an unlikely integer
                        echo paginate_links( 
                            array(
                                'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                                'format'  => '/page/%#%',
                                'current' => max( 1, get_query_var('paged') ),
                                'total'   => ceil( $number_of_terms / $per_page ) // 20 items per page
                            ) 
                        );
                        ?>
                    </div>
                </article>
            </span>
        </div> <!-- End col-md-9 -->

        <div class="col-md-3">
            <?php include("sidebar100list.php"); ?>
        </div>  <!-- End col-md-3 -->

    </div><!-- End row -->
</div><!-- End Container -->
<?php get_footer(); ?>
0
0

The problem is with function_exists("pagination"). There is no function named pagination in Wordpress. You are using the function paginate_links(). So, you should Replace that with

function_exists("paginate_links")
1
  • I thought that was going to work, but I don't see any change when I update that function. :( Still no page links showing.
    – streetfire
    Commented Aug 21, 2014 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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