Page pagination Category.php in Wordpress

Asked

Viewed 1,650 times

1

I am trying to activate Wordpress pagination in a theme’s Category.php file, but the pagination links do not appear. From the little I researched, it would have something to do with the use of custom queries, but I didn’t quite understand its use.

How I could activate paging (either native or with the WP Postnavi plugin) in my page code, shown below?

<?php get_header(); ?>  
  <main>
  <div id="content_internas">
    <div id="lista_noticias">
      <h2><span><?php echo strtoupper(single_cat_title( '', false )) ?></span></h2>
      <ul>
          <?php $posts = get_all_posts(null, get_cat_id( single_cat_title("",false) )); ?>

          <?php foreach($posts as $key => $post): ?>
        <li>
          <a href="<?php the_permalink() ?>">
            <?php if (function_exists('has_post_thumbnail')) {
              if (has_post_thumbnail()) { ?>
              <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(460, 300, true)); ?></a>
              <?php } } ?>
          </a>
          <br />
          <span><?php the_time('j F') ?></span>
          <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
          <p><?php the_excerpt(); ?></p>
        </li>
        <?php endforeach; ?>
      </ul>
      <div class="paginacao">
        <?php if(function_exists('wp_pagenavi')) : ?>
          COM PLUGIN
          <?php wp_pagenavi() ?>
        <?php else : ?>
          SEM PLUGIN
          <div class="maisantigos"><?php next_posts_link(__('&laquo; Mais antigos','arclite')) ?></div>
          <div class="maisrecentes"><?php previous_posts_link(__('Mais recentes &raquo;','arclite')) ?></div>
        <?php endif; ?>
      </div>
      <!--<center><a href="#" id="veja_mais">MAIS NOTÍCIAS</a></center>--><br> <br>
    </div>
  </div>
</main>
<?php get_footer(); ?>
  • 1

    Some information is missing: is the wp_pagenavi plugin active and configured to be displayed even when there is only one page? It simply doesn’t display or display an error message instead? In the code you posted, you see the message "WITH PLUGIN"?

  • 1

    Please put your code here, if the Pastebin disappear the question loses its meaning. You can [Dit] the post and include the code.

  • Sorry for the lack of information. Actually my intention was to make the code redundant, can be used with or without Pagenavi.

1 answer

4


There is no function get_all_posts() in Wordpress, try to search for it: http://developer.wordpress.org/reference/

Probably some custom function, where the global variable is not configured $wp_query and why paging does not work.

How you want to use the category.php the correct is to use the native Wordpress query. In this case just use as:

<?php get_header(); ?>
<main>
<div id="content_internas">
    <div id="lista_noticias">
        <h2><span><?php echo strtoupper(single_cat_title( '', false )) ?></span></h2>
        <ul>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <li>
                <a href="<?php the_permalink() ?>">
                    <?php if ( has_post_thumbnail() ) : ?>
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array( 460, 300, true ) ); ?></a>
                    <?php endif; ?>
                </a>
                <br />
                <span><?php the_time( 'j F' ); ?></span>
                <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
            </li>
            <?php endwhile; else: ?>
                <li><?php _e( 'Sorry, no posts matched your criteria.' ); ?></li>
            <?php endif; ?>
        </ul>
        <div class="paginacao">
            <?php if ( function_exists( 'wp_pagenavi' ) ) : ?>
                COM PLUGIN
                <?php wp_pagenavi() ?>
            <?php else : ?>
                SEM PLUGIN
                <div class="maisantigos"><?php next_posts_link(__('&laquo; Mais antigos','arclite')) ?></div>
                <div class="maisrecentes"><?php previous_posts_link(__('Mais recentes &raquo;','arclite')) ?></div>
            <?php endif; ?>
        </div>
    </div>
</div>
</main>
<?php get_footer(); ?>

You can find more details on how to work with Wordpress loop right here.

Note 1: In your code you tested if there was a function has_post_thumbnail(), note that it has been implemented in Wordpress 2.9, which had its release date on December 18, 2009. In other words, you do not need to ever test if it exists, no one will be using such an outdated Wordpress like this.

Note 2: If you want you don’t have the slightest need to use WP-Paginavi, at least not since January 22, 2007 when the Wordpress 2.1 and together was implemented the function paginate_links().

  • 2

    This little guy who is talking knows all about Wordpress. A hug Claudio who helped me a few times in the Facebook community.

  • 1

    Thank you very much, Claudio! Your solution worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.