Search code examples
phpwordpress

Wordpress foreach loop in pagination query


I'm trying finish a query using something called a foreach loop but I can't for the life of me get it to work. Can anyone help me finish this.

They pagination query at the top is complete, but you can see in the bottom loop, where by if it displays no pagination links then it will not output any html markup. But if pagination does exist, I can't get it to output the pagination links using a foreach loop.

This question is an extension of this. https://stackoverflow.com/questions/8726656/

<?php

    global $wp_query;

    $big = 999999999; // need an unlikely integer

    $links = paginate_links( array(
        'base'      => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format'    => '?paged=%#%',
        'current'   => max( 1, get_query_var('paged') ),
        'total'     => $wp_query->max_num_pages,
        'prev_text' => __('&#8592; previous downloads','help'),
        'next_text' => __('newer downloads &#8594;','help'),
        'type'      => 'array'
    ));

?>

<?php if (count($links) > 0) : ?>

   <div class="archive-navigation">
     <?php

     // Display links using a foreach loop.

     ?>
   </div>

<?php endif ?>

Solution

  • In php, a foreach is a looping construct that binds the "current" item to a variable name. Its like saying "do this procedure for each of these items". Try the following:

    <?php
       foreach($links as $link) {
         echo $link;
       }
     ?>
    

    Here, $links should be an array of Strings, although the documentation for paginate_links() isn't crystal clear about this. If you wanted you could echo additional markup in the foreach to help customize the links. To target these links using css:

    .archive-navigation a {
        // styles, e.g.
        margin-right: 5px;
    }