Custom pagination - arrows not working - php

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;

Related

Changing woocommerce pagination structure

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>

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.

WordPress Pagination buttons show the same posts on all pages

I am having an issue with the pagination on my WordPress blog page. The next and previous page buttons take me to a new page but all pages show the same 10 blog posts.
Here is the code I am using:
<?php
$args = array('cat' => '-27');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = '?&paged=%#%';
} else {
$format = 'page/%#%/';
}
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
?>
Any help would be greatly appreciated!

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;
}

Trying to achieve WP Pagination with only Prev and Next buttons

I'm trying to modify my code for pagination in such a way that it only displays the Prev and Next buttons with no pages in the middle. This is proving to be kind of hard because paginate_links function doesn't have many options in terms of arguments which I can pass to disable the pages from being displayed.
Also using next_posts_link() and previous_posts_link() functions doesn't really help because that links to mydomain.com/page/2 whereas my default homepage is something like this mydomain.com/?fp_type=hot (to display particular kind of custom posts)
This is my pagination code
<?php
global $wp_query;
global $wp_rewrite;
if( is_search() ){
if( (int) get_query_var('page') > 0 ){
$current = get_query_var('page');
}else{
if( (int) get_query_var('paged') > 0 ){
$current = get_query_var('paged');
}else{
$current = 1;
}
}
$wp_query = new WP_Query('paged='. $current . '&s='.get_query_var('s') );
$pagination = array(
'base' => #add_query_arg( 'paged' , '%#%' ),
'format' => '',
'total' => $wp_query -> max_num_pages,
'current' => $current,
'show_all' => false,
'prev_next'=> true,
'prev_text'=> __('« Previous','cosmotheme'),
'next_text'=> __('Next »','cosmotheme'),
'type' => 'array'
);
if( $wp_rewrite->using_permalinks() ){
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'search', remove_query_arg( 's', get_pagenum_link( 1 ) ) ) ) . 'page/%#%/', 'paged' );
}
if( !empty($wp_query->query_vars['s'] ) ){
$pagination['add_args'] = array( 's' => urlencode( get_query_var( 's' ) ) );
}
$pgn = paginate_links( $pagination );
if( !empty( $pgn ) ){
echo '<div class="pag">';
echo '<ul class="b_pag center p_b">';
if( $current == 1 ){
$current--;
}
foreach($pgn as $k => $link){
print '<li>' . str_replace("'",'"',$link) . '</li>';
}
echo '</ul>';
echo '</div>';
}
}else{
$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,
'show_all' => false,
'type' => 'array'
);
if( $wp_rewrite->using_permalinks() ){
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'fp_type' , remove_query_arg( 'type' , remove_query_arg( 's', get_pagenum_link( 1 ) ) ) ) ) . 'page/%#%/', 'paged' );
}
if( !empty($wp_query->query_vars['s'] ) ){
$pagination['add_args'] = array( 's' => urlencode( get_query_var( 's' ) ) );
}
if( !empty( $wp_query->query_vars['type'] ) ){
$pagination['add_args'] = array( 'type' => get_query_var( 'type' ) );
}
if( !empty( $wp_query->query_vars['fp_type'] ) ){
$pagination['add_args'] = array( 'fp_type' => get_query_var( 'fp_type' ) );
}
$pgn = paginate_links( $pagination );
if( $current == 1 ){
$current--;
}
if(!empty($pgn)){
echo '<div class="pag">';
echo '<ul class="b_pag center p_b">';
foreach($pgn as $k => $link){
print '<li>' . str_replace( "'" , '"' , $link ) . '</li>';
}
echo '</ul>';
echo '</div>';
}
} ?>
Add this code to that page where you want to show the post.
<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat' => '7',
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $page,
);
query_posts($args);?>
add below code after the post code:
<?php while ( have_posts() ) : the_post(); ?>
<div>
<ul>
<li>
<?php the_content(); ?>
</li>
</ul>
</div>
<?php endwhile; // end of the loop. ?>
<div class="navigation"><p><?php posts_nav_link(); ?></p></div>

Categories