Ask questions about WordPress courses

Add pagination to blog

Add the first block of code in functions.php your topic WordPress and add my_paginate_links() instead of, your topic, where to display navigation. After adding the code, the pagination will appear in the right place.

   function my_paginate_links() {
   global $wp_rewrite, $wp_query;
   $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
   $pagination = array(
     'base' => @add_query_arg('paged','%#%'),
     'format' => '',
     'total' => $wp_query->max_num_pages,
     'current' => $current,
     'prev_text' => __('« Previous'),
     'next_text' => __('Next »'),
     'end_size' => 1,
     'mid_size' => 2,
     'show_all' => true,
     'type' => 'list'
   );
   if ( $wp_rewrite->using_permalinks() )
   $pagination['base'] = user_trailingslashit( trailingslash( 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' ) );
   echo paginate_links( $pagination );
   }
<? my_paginate_links(); ?>

In addition, you can always use the plugin WP-PageNavi to add pagination and plugin WP PageNavi Style for numbering.

Bonus code

On the row with the code, which we examined, I decided to add one more which is the code. This code is too short, to put it in a separate article, but also too good, to forget about him. The code will definitely come in handy.
Before, I would do the count of published articles through a database query, this would work well. On the other hand, now I want to show you how to make this task easier.. Consider the standard code for displaying the number of published articles on the blog.

<?
            $published_posts = wp_count_posts()->publish;
            echo $published_posts;
?>

That's all, I checked all codes, and it remains only to wish good luck in the development of sites.


Купить хостинг WordPress
WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

2 Responses

  1. Yuri says:

    Nice in the end you get it. I did the numbering with a plug-in and I don’t really like it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal