Changing woocommerce pagination structure - php

I have searched all over for solutions, but I can't find one that actually helps me.
On my store, when you hover over a woocommerce pagination link, the pagination format looks like this: mycategory/page/2.
However, once you click on it the URL in the address bar shows mycategory/?_paged=2.
(It almost seems as if the one redirects to the other, which means the way the pagination is linked needs to change.)
I want the pagination to be linked the same as the actual pagination URLs (mycategory/?_paged=2)
I have searched through the woocommerce template files but I can't figure out how to change it. I think it must be changed in this code, but I can't seem to figure it out. Is it the $format variable that needs to be edited?
$total = isset( $total ) ? $total : wc_get_loop_prop( 'total_pages' );
$current = isset( $current ) ? $current : wc_get_loop_prop( 'current_page' );
$base = isset( $base ) ? $base : esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) );
$format = isset( $format ) ? $format : '';
if ( $total <= 1 ) {
return;
}
?>
<nav class="woocommerce-pagination">
<?php
echo paginate_links(
apply_filters(
'woocommerce_pagination_args',
array( // WPCS: XSS ok.
'base' => $base,
'format' => $format,
'add_args' => false,
'current' => max( 1, $current ),
'total' => $total,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3,
)
)
);
?>
</nav>

Related

WooCommerce | How do I add title tag to woocommerce pagination links

I am trying to solve a problem on my website for SEO reasons. I am working with WooCommerce. At the bottom of each category page there is a pagination with links (1 2 3 4). I would like to add a title tag to the HTML link.
How can I add the title attribute to the pagination links?
Thanks a lot in advance!
The plugin file I have tried to edit is located in woocommerce/templates/loop/pagination.php and looks like this:
``<?php /** * Pagination - Show numbered pagination for catalog pages * * This template can be overridden by copying it to yourtheme/woocommerce/loop/pagination.php. * * HOWEVER, on occasion WooCommerce will need to update template files and you * (the theme developer) will need to copy the new files to your theme to * maintain compatibility. We try to do this as little as possible, but it does * happen. When this occurs the version of the template file will be bumped and * the readme will list any important changes. * * #see https://docs.woocommerce.com/document/template-structure/`
* #package WooCommerce\Templates
* #version 3.3.1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$total = isset( $total ) ? $total : wc_get_loop_prop( 'total_pages' );
$current = isset( $current ) ? $current : wc_get_loop_prop( 'current_page' );
$base = isset( $base ) ? $base : esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) );
$format = isset( $format ) ? $format : '';
if ( $total <= 1 ) {
return;
}
?>
<nav class="woocommerce-pagination">
<?php
echo paginate_links(
apply_filters(
'woocommerce_pagination_args',
array( // WPCS: XSS ok.
'base' => $base,
'format' => $format,
'add_args' => false,
'current' => max( 1, $current ),
'total' => $total,
'prev_text' => is_rtl() ? '→' : '←',
'next_text' => is_rtl() ? '←' : '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3,
)
)
);
?>
</nav>

Paginate WPQuery redirect to page 1

In my Wordpress template, I'm making a WP query in order to list all cities in specific regions (custom post type).
In some cases, regions have more than 3000 cities so the Query is too heavy and runs out of memory.
Therefore I'm using the paginate_links function in order to paginate each list when there are more than 500 results.
It works fine but the paginate links are somehow redirected to the main page.
Examples :
http://www.example.fr/my-region/page/2
redirect to
http://www.example.fr/my-region so I can never access to the paginated pages.
Here is my code (partial).
Do you see what may cause this problem?
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = array(
'post_type' => 'commune',
'posts_per_page'=> 500,
'paged' => $paged,
'orderby' => 'title',
'order'=> 'ASC',
'meta_key' => 'region',
'meta_value' => $reg_region
);
$communes = new WP_Query($posts );
$nb_communes = $communes-> found_posts;
?>
<div class="col-8 main-content">
<?php while (have_posts()) : the_post(); ?>
<?php
echo "<ul class=\"list-group\">";
while ($communes->have_posts() ) { $communes->the_post();
?>
<li class="list-group-item"><?php echo(get_field("commune")); ?> (<?php echo(get_field("cp")); ?>) - <?php echo(get_field("civilite")); ?> <?php echo(get_field("prenom")); ?> <?php echo(get_field("nom")); ?> </li>
<?php
}
echo "</ul>";
?>
<?php endwhile; // end of the loop. ?>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $communes->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 500,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Précédent', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Suivant', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
I do not know if it's related but in my functions.php file, I have this override so that my cystompost type skip the /$name_of_custom_post_type/ in URLs.
/**
* Remove the slug from published post permalinks. Only affect our custom post type, though.
*/
function gp_remove_cpt_slug( $post_link, $post, $leavename ) {
$custom_post_types = array('departement', 'region', 'commune');
if ( ! in_array( $post->post_type, $custom_post_types ) || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );
/**
* Have WordPress match postname to any of our public post types (post, page, race)
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* By default, core only accounts for posts and pages where the slug is /post-name/
*/
function gp_parse_request_trick( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array('post', 'departement', 'region', 'commune' ) );
}
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );

Pagination not working for custom post type

Video Explanation https://drive.google.com/open?id=0B4HLAyVud6iLNjlHMGF3RXA3Qkk
I am trying to make a pagination, so far it is showing page links properly like 1 2 3 4 > but the problem is when i navigate to page 2 or any other page it take me to 404 page. Here is my pagination function
if ( ! function_exists( 'sp_pagination' ) ) {
function sp_pagination( $query = '', $echo = true ) {
$prev_arrow = is_rtl() ? 'fa fa-angle-right' : 'fa fa-angle-left';
$next_arrow = is_rtl() ? 'fa fa-angle-left' : 'fa fa-angle-right';
// Get global $query
if ( ! $query ) {
global $wp_query;
$query = $wp_query;
}
// Set vars
$total = $query->max_num_pages;
$big = 999999999;
// Display pagination
if ( $total > 1 ) {
// Get current page
if ( $current_page = get_query_var( 'paged' ) ) {
$current_page = $current_page;
} elseif ( $current_page = get_query_var( 'page' ) ) {
$current_page = $current_page;
} else {
$current_page = 1;
}
// Get permalink structure
if ( get_option( 'permalink_structure' ) ) {
if ( is_page() ) {
$format = 'page/%#%/';
} else {
$format = '/%#%/';
}
} else {
$format = '&paged=%#%';
}
$args = apply_filters( 'sp_pagination_args', array(
'base' => str_replace( $big, '%#%', html_entity_decode( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, $current_page ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => '<span class="' . $prev_arrow . '"></span>',
'next_text' => '<span class="' . $next_arrow . '"></span>',
) );
// Output pagination
if ( $echo ) {
echo '<div class="sp-pagination">' . paginate_links( $args ) . '</div>';
} else {
return '<div class="sp-pagination">' . paginate_links( $args ) . '</div>';
}
}
}
}
And here is my query
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$portfolio_grid_query = new WP_Query(
array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'name' => $portfolio_grid_title_slug,
'posts_per_page' => $portfolio_grid_item_limit,
'category_name' => $portfolio_grid_cats,
'order' => $portfolio_order,
'orderby' => $portfolio_orderby,
'paged' => $paged
)
);
here all the while loop and post structure goes and after while loop ends i put the pagination function like so:
sp_pagination($portfolio_grid_query, true);
It is showing all the navigation structure properly but the issue is when i navigate it takes me to 404 page instead of page 2 to show other remaining posts. Keep in mind i have total of 9 posts and i set posts_per_page to 6, which is showing me 6 posts on current page and if i click on page 2 link it should take me to other page to show remaining 3 posts.

How to display post ranges instead of page numbers in Wordpress pagination?

How to output posts range contained in each step of pagination instead of just current page number ?
I mean:
1-10 11-20 21-30 31-40... 101-110 Next
instead of :
1 2 3 4 5 6 .. 10 Next
My current pagination function:
function wp_pagination() {
global $wp_query;
$total = $wp_query->max_num_pages;
$prev_arrow = is_rtl() ? '<span class="next">Next</i>' : '<span class="previous">Prev</span >';
$next_arrow = is_rtl() ? '<span class="previous">Prev</i>' : '<span class="next">Next</span >';
$big = 999999999;
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
Unfortunately, the paginate_links function does not allow you to change the format of displayed page numbers.
Source of paginate_links function: https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/general-template.php#L2742
Following line is responsible to build page numbers:
$page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>" . $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] . "</a>";
As you can see, the page number simply inserted as content of the <a> tag without any possibility to change displayed format - number_format_i18n( $n )
To achieve your goal, you should create your own implementation of this function. Use the original wp paginate_links function as an example.
For anyone still looking for a way to achieve this. This is how I did it:
$links = paginate_links( array(
'base' => esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
'format' => '',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $query->max_num_pages,
'prev_next' => false,
'end_size' => 4,
'mid_size' => 4,
'type' => 'array',
));
$i = 0;
$current_page = max( 1, get_query_var( 'paged' ) );
foreach( $links as $link ){
$startrange = $i + 1;
$endrange = $i + 10;
// is active ?
if($links[$current_page-1] == $link){
echo preg_replace('/<span(.+?)>.+?<\/span>/i',"<span$1>$startrange - $endrange</span>",$link);
}else{
echo preg_replace('/<a(.+?)>.+?<\/a>/i',"<a$1>$startrange - $endrange </a>",$link);
}
$i += 10;
}

Custom pagination - arrows not working

I have a strange problem where the arrows generated for next & previous pages don't work. I just can't seem to fix it. The html seems to be fine and numbers work, but when clicked on nothing happens: http://sketchbookmagazine.com/category/fashion/
Pagination code:
function pagination($prev = '«', $next = '»') {
global $wp_query, $wp_rewrite;
$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' => __($prev),
'next_text' => __($next),
'type' => 'plain'
);
if( $wp_rewrite->using_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( 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 );
};
html:
<div class="pagination"><?php pagination('«','»'); ?></div>
Any help is greatly appreciated!
I have never seen this construct:
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
Try changing that to:
$current = $wp_query->query_vars['paged'] > 1 ? $wp_query->query_vars['paged'] : 1;

Categories