Comments pagination on author page - Wordpress - wordpress

I have author page (example.com/author/joe) on it I have listed all author's comments with pagination but pagination doesn't work after clicking the url - example.com/author/joe/page/2 and page is redirect to index.php.
In other pages it works, Facing problem in this author page only.
Any ideas ?
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$number = get_option('comments_per_page');
$offset = ( $paged - 1 ) * $number;
$count = count( get_comments( array(
'user_id' => $user_ID,
'status' => 'approve',
) ) );
$max_total = ceil($count/$number);
$args = array(
'user_id' => $user_ID,
'number' => $number,
'status' => 'approve',
'offset' => $offset,
'paged' => $paged
);
$the_query = new WP_Comment_Query;
$comments = $the_query->query( $args );
foreach($comments as $comment){
//code
}
//Pagination
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $max_total,
'prev_next'=> true,
) );
Matt

Related

How to add title attribute for <a> tags to show tooltips for wordpress paginate_links function

In order to enhance user-experience i’d like to add the title attribute to the «a» tags in the paginate_links. For the class «page-numbers» a title like «page X» and for the class «next» a title like «next page».
I’am using paginate_links function:
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => $ppframe,
'category_name' => $cat_id,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
$big = 999999999; // need an unlikely integer
echo '<div class="archiv-pager">';
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'end_size' => 3,
'mid_size' => 1,
'before_page_number' => '',
'after_page_number' => ' • '
));
echo '</div>';
I’ve searched the wordpress documentation and also various tutorials about paginate_links but couldn’t find a hint how to add the title attribute to the a tags of the paginate links. Is there a specific argument for paginate_links function or some other method to add the title attribute? Thanks for any hint.

wordpress custom post type pagination 404 page 2 error

In my Wordpress custom theme, a pagination giving the 404 error when loading the page above 1. I have checked out the Permalinks and other stuff but no scuccess.
Please help me to get rid off.
Here is my code.
//Loop
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'orderby' => 'date',
'post_type' => 'jobs',
'posts_per_page' => 1,
'paged' => 'paged'
);
$the_query = new WP_Query( $args );
//Pagination
$total = $the_query->max_num_pages;
$big = 999999999; // need an unlikely integer
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,
) );
}
please add this code to function.php file in your theme file
flush_rewrite_rules( false );

Wordpress pagination does not show

For a new WordPress template I coded a loop that works fine. But there is no pagination. Does anyone see, why there is no pagination shown?
Thanks!
My loop:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'numberposts' => -1,
'posts_per_page' => 9,
'paged' => $paged,
'cat' => '4'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
......
<?php
}
} else { echo 'no posts found'; }
wp_reset_postdata();
?>
And on the bottom of the page:
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
Please try this.
if ( ! function_exists( 'pagination' ) ) :
function pagination( $paged = '', $max_page = '' ){
global $wp_query
$big = 999999999; // need an unlikely integer
if( ! $paged )
$paged = get_query_var('paged');
if( ! $max_page )
$max_page = $wp_query->max_num_pages;
echo paginate_links( array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link( $big ))),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $max_page,
'mid_size' => 2,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'list'
) );
}
endif;
And the loop template will be like -
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $args = array(
''post_type' => 'post',
'posts_per_page' => 9,
'paged' => $paged,
'cat' => '4'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
pagination($paged, $loop->max_num_pages);// Pagination Function
endif;
wp_reset_postdata();

I can't get pagination to work in WP query

This is what my current wordpress post query looks like:
<?php
$new_loop = new WP_Query( array(
'post_type' => 'news',
'posts_per_page' => 5
) );
?>
I want to add the following pagination to it:
<?php the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => __( 'Prev'),
'next_text' => __( 'Next'),
) ); ?>
I googled for various solutions. Everywhere it said to add "paged" to the array, like so:
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;**
$new_loop = new WP_Query( array(
'post_type' => 'news',
'paged' => $paged,**
'posts_per_page' => 5 // put number of posts that you'd like to display
) );
?>
However, this does not work. How can I get the pagination to work in a custom wordpress post query?
I think you're missing the argument: 'current' => max( 1, get_query_var( 'paged' ) )
After you run your loop, you can add this function in your theme(functions.php or elsewhere):
if ( ! function_exists( 'custom_pagination' ) ) {
function custom_pagination( $args = array(), $class = 'pagination' ) {
if ( $GLOBALS['wp_query']->max_num_pages <= 1 ) {
return;
}
$args = wp_parse_args(
$args,
array(
'mid_size' => 3,
'prev_next' => true,
'prev_text' => __( 'Previous', 'theme' ),
'next_text' => __( 'Next', 'theme' ),
'screen_reader_text' => __( 'Posts navigation', 'theme' ),
'type' => 'array',
'current' => max( 1, get_query_var( 'paged' ) ),
//'total' => $the_query->max_num_pages,
)
);
$links = paginate_links( $args );
?>
<nav aria-label="<?php echo $args['screen_reader_text']; ?>">
<ul class="pagination">
<?php
foreach ( $links as $key => $link ) {
?>
<li class="page-item <?php echo strpos( $link, 'current' ) ? 'active' : ''; ?>">
<?php echo str_replace( 'page-numbers', 'page-link', $link ); ?>
</li>
<?php
}
?>
</ul>
</nav>
<?php
}
}
Then finally call it in whatever template you need to:
custom_pagination();

404 error on pagination for custom post type, custom taxonomies

Page numbers and buttons display on a custom post type archive and the archives for custom taxonomies but render 404.
What am I doing wrong?
Here's the query for the custom post type archive:
<?php $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
'post_type' => 'tracker',
'publish_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
) );
}
if ( $exec_query->have_posts() ) { ?><?php global $wp_query; $posts_per_page = $wp_query->query["posts_per_page"]; $posts_found = $wp_query->found_posts; ?><?php while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>
This is the pagination code that I am using for the custom post type archive:
<section class="pagination">
<?php global $exec_query;
$big = 999999999;
$translated = __( 'Page', 'theme-name' );
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url (get_pagenum_link( $big )) ),
'format' => 'page/%#%/',
'current' => max( 1, get_query_var('paged') ),
'prev_text' => __('« Previous'),
'next_text' => __('Next »', $exec_query->max_num_pages),
'total' => $exec_query->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.'</span> '
) );
?>
</section>
Here's the query for one of the custom taxonomies:
<?php $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $post_type = get_queried_object(); echo $post_type->rewrite['slug']; $loop = new WP_Query( array(
'post_type' => 'tracker',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'taxonomy1',
'field' => 'slug',
'terms' => $post_type,
),
),
) ); if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
This is the pagination code for one of the taxonomies:
<section class="pagination">
<?php global $loop;
$big = 999999999;
$translated = __( 'Page', 'theme-name' );
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url (get_pagenum_link( $big )) ),
'format' => 'page/%#%/',
'current' => max( 1, get_query_var('paged') ),
'prev_text' => __('« Previous'),
'next_text' => __('Next »', $loop->max_num_pages),
'total' => $loop->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.'</span> '
) );
?>
</section>

Resources