How to implement Wordpress pagination? - wordpress

I'm trying to implement pagination for my posts. Though I'm a bit stuck on the php function and also how to call it.
The simple method is this I guess:
<?php posts_nav_link(); ?>
But what if I want custom pagination?
Here is my current code:
<?php
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
?>
Is this correct and how do I call it? In index.php? Where in the loop? Thanks.
Edit for clarification: How do I implement this code? At the moment I have put it in my functions.php. So how (and where in the loop) do I 'reference' this function so the pagination is displayed.

There are two ways you can implement this code. It looks like right now you are sort of in between the two.
The first way would be to add your pagination code directly into the template that it will be used in somewhere inside the loop (most likely somewhere right before the closing <?php endwhile; ?> tag). If you are using a single.php template, you would put it in there, if not, put it in index.php. The placing of it inside of the loop depends on where you want the pagination to appear on your page.
The second way is to add the pagination code to the functions.php file (which you have done). However, you will need to revise your code a bit for this. You need to wrap the code within a function, and name that function something. I've used your_custom_pagination for an example. Your functions.php file is most likely already wrapped in php tags, so I have removed those.
function your_custom_pagination() {
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') ) {
$current_page = 1;
}
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
}
Then you'll need to go into the template that you are using and place this code <?php your_custom_pagination(); ?> into the same spot I illustrated above to call the pagination function.
I haven't actually tested your code, so assuming it's valid everything should work.

Related

change pagination link in WordPress

I am getting pagination links like -> /page/3. But i would like to change this "page" to "news". Is it possible?
Thank you!
echo paginate_links( array(
'base' => str_replace( $big2, '%#%', esc_url( get_pagenum_link( $big2 ) ) ),
'format' => '?paged1=%#%',
'current' => max( 1, get_query_var('paged1') ),
'total' => $news->max_num_pages,
'mid_size' => 0,
'type' => 'plain',
'end_size'=>0,
'prev_text' => '<IEPREKŠĒJĀ LAPA',
'next_text' => 'NĀKOŠĀ LAPA>'
) );
function my_custom_pagination_base() {
global $wp_rewrite;
$wp_rewrite->pagination_base = 'p'; //where new-slug is the slug you want to use
$wp_rewrite->flush_rules();
}
add_action('init', 'my_custom_pagination_base', 1);
You can change pagination base using below code
<?php
add_action( 'init', 'my_custom_page_word' );
function my_custom_page_word() {
global $wp_rewrite; // Get the global wordpress rewrite-rules/settings
// Change the base pagination property which sets the wordpress pagination slug.
$wp_rewrite->pagination_base = "news"; //where new-slug is the slug you want to use ;)
}
?>
For reference you can refer below link
https://wordpress.stackexchange.com/questions/57070/change-the-page-slug-in-pagination
To remove page or whatever your base is set to for your pagination do the following.
First add the following to functions.php. This function will rewrite our base pagination prefix to and empty string.
function my_custom_pagination_base() {
global $wp_rewrite;
$wp_rewrite->pagination_base = '';
$wp_rewrite->flush_rules();
}
add_action('init', 'my_custom_pagination_base', 1);
Second we add the our paginate_links() function to our archives page. with the following arguments. Make sure base is not set, or it will mess up our rewrite function.
echo paginate_links(array(
'format' => '\\%#%/',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
));
Set your current and total args to whatever you need them to be.
Your archive pagination url should look like www.site.com/some-category/2/ when you go to page 2

Wordpress search term pagination broken

I'm having an issue with the pagination in search archives. For some reason whenever I try going to the next page the pagination gives me a 404.
It's a default if (have_posts()) : while (have_posts()) : the_post(); loop.
I don't know how, but I think the reason is because I'm getting redirected to a new page that get's generated somehow. Say I search for stoff. Instead of /page/2/?s=stoff it replaces it with ?attachment_id=550&s=stoff.
Here's what I've tried to do (with no different results)
Change the search query (attachment_id is the same here as well)
Change pagination functions (pagenavi, get_next_posts_link, custom function)
Replacing loop content with just a title
removing header, sidebars, footer
resetting post data
Resetting permalinks (pretty)
You can test the pagination live here (dev location)
Any thoughts on this are greatly appreciated :)
EDIT -
Added Pagination code currently used in search.php
I don't think it should be a problem with the pagination, seeing that I've tried so many different ones, and this pagination still works in every other location at the site, but here it is:
// Numeric Page Navi (built into the theme by default)
function bones_page_navi() {
global $wp_query;
$bignum = 999999999;
if ( $wp_query->max_num_pages <= 1 )
return;
echo '<nav class="pagination">';
echo paginate_links( array(
'base' => str_replace( $bignum, '%#%', html_entity_decode( get_pagenum_link($bignum) ) ),
'format' => '',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) );
echo '</nav>';
} /* end page navi */
The function get's called after the endwhile;

Trouble with pagination wordpress category page

I am in trouble with the category page in wordpress, I want to display the pagination and I used a method that works in other templates that I developed in the past.
I get the Category ID
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
Declare the pagination
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wpquery = new WP_Query(array(
'order' => 'DESC',
'cat' => $cat_id,
'posts_per_page' => 4,
'paged'=>$page
));
And after my Loop I show the pagination
global $wpquery;
if( $wpquery->max_num_pages >1){
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wpquery->max_num_pages
) );
}
If you try to go here
http://192.185.20.70/~t1t/tobeus.it/category/press/
you can see that the first & second page works, the third shows a 404...
in the other categories like "events"
http://192.185.20.70/~t1t/tobeus.it/category/events/
only the first page works...
I can't solve this strange problem!
I also had a problem with pagination in category template.
Try to change 'format' parameter 'paged' of paginate_links function to any other.
Example:
Set other parameter name, for example 'newparam'
echo paginate_links( array(
...
'format' => '?newparam=%#%',
Then get it from your url
$page = (get_query_var('newparam')) ? get_query_var('newparam') : 1;
Also don't forget to declare 'newparam' in functions.php file 'add_query_vars_filter' function.
Other solutions didn't worked for me, but this one did. I cannot explain exactly why, but it might be that parameter 'paged' is used somewhere when the second page is loading or something..
In this example you will just use other variable name to pass page number, and when the other pagination page will load, you will assign that value to 'paged' again, so it will load correct data from database.

Main loop with custom post type added - paginate_links doesn't work well

I have such a piece of code
<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'post_type' => array( 'post', 'project') ) );
$wp_query = new WP_Query( $args );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
after the loop i have
<?php
$permalink_structure = get_option('permalink_structure');
$format = empty( $permalink_structure ) ? '?paged=%#%' : 'page/%#%/';
echo paginate_links( array(
'base' => get_pagenum_link(1) .'%_%',
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => __('«'),
'next_text' => __('»'),
'show_all' => false,
'mid_size' => 2,
'end_size' => 1,
) );
?>
now the problem is that if I have 6 posts and 18 projects and 3 posts per page ... paginate_links will generate (6+18)/3 pages i.e. 8 pages ... so I click on 2 and go to page number 2 .. but when I click on 3 .. I get error 404.
As if paginate_links generates the required amound of page links but only the links to 6/3 pages word .. like 1 and 2.
Problem is for sure because of custom post type added but I can't understand where is that problem.
What may be the problem?
It looks like you have to alter the "main query" (you are using a "sub query" inside the "main query") to include your custom post type, so your pagination links will work.
You can try to alter the "main query" using the pre_get_posts hook
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $query ) {
if($query->is_main_query() && $query->is_home()){ // <-- EDIT this condition to your needs
$query->set( 'post_type', array( 'post','projects' ) );
}
}
where you place this code into the functions.php file in your current theme directory.
This assumes you are using the pagination on the frontpage, i.e.
http://example.com/page/5
We have the condition $query->is_home() to check if we are on the frontpage. If you are on a different page, you can alter this condition to your needs.
ps: I think your way is not working because you are doing it in the theme file and that is "too late" to alter the scope of the pagination links.

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' => __('← previous downloads','help'),
'next_text' => __('newer downloads →','help'),
'type' => 'array'
));
?>
<?php if (count($links) > 0) : ?>
<div class="archive-navigation">
<?php
// Display links using a foreach loop.
?>
</div>
<?php endif ?>
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;
}

Resources