Wordpress pagination loop show pagination html if

2019-06-07 21:35发布

I'm using a pagination query in my loop. But I need it to output HTML only if the pagination is required. Because on some pages, the pagination markup will not be necessary because the page will only have a few posts.

So currently in my loop, if the archive page shows 8 posts, my markup <div class="archive-navigation"> and </div> is still being outputted. How can I weave this HTML markup into my pagination query so it only outputs the HTML if pagination is required?

The pagination and markup will only ever be required if the loop calls more that 10 posts.

Thanks in advance.

<?php query_posts(array(

    'post_type' => 'download',
    'order' => 'DESC',
    'paged' => $paged,
    'posts_per_page' => 10

)); ?>

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

     <!-- MY LOOP -->

<?php endwhile; ?>

    <div class="archive-navigation">

    <?php   
        global $wp_query;
        $big = 999999999; // need an unlikely integer
        echo 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','multilingol'),
        'next_text' => __('newer downloads &#8594;','multilingol')
        ));
    ?>

    </div>

<?php endif; wp_reset_query(); ?>

标签: wordpress
1条回答
我想做一个坏孩纸
2楼-- · 2019-06-07 22:21

You can add the type => 'array' to the arguments like below.

<?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','multilingol'),
  'next_text' => __('newer downloads &#8594;','multilingol'),
  'type'      => 'array',
));
?>

After this you can check with count($links) if the array has links to display or not the <div>.

The final code will look like this:

<?php   
global $wp_query;
$big = 999999999; // need an unlikely integer
$links = paginate_links( ... )); // Above parameters array.
?>

<?php if (count($links) > 0) : ?>
<div class="archive-navigation">
  <?php foreach ($links as $link) : ?>
  <?php echo $link ?>
  <?php endforeach ?>
</div>
<?php endif ?>

For more information on use paginate_links function look here: http://codex.wordpress.org/Function_Reference/paginate_links

查看更多
登录 后发表回答