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

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.

Related

Pagination through shortcode (custom post type) returns the same result in every page

The problem I'm having is that pagination (which I'm running through a shortcode) is not working as expected. In every paginated page, the results are the same. I've found similar questions about problems such as this one but none of the suggestions I've tried seem to work.
Pagination is showing up correctly, page count is correct and the URL is changing as expected when changing the page but the returned items are always the same in every page.
My current code:
function shortcode_paginated_list() {
$args = array(
'post_type' => 'my_custom_post_type',
'posts_per_page' => '2',
'publish_status' => 'published',
'paged' => '<li>'.( get_query_var('paged') ? get_query_var('paged') : 1).'</li>'
);
$wp_query = new WP_Query($args);
if($wp_query->have_posts()) :
while($wp_query->have_posts()) : $wp_query->the_post();
$result = 'fetching some stuff';
endwhile;
endif;
$big = 999999999; // need an unlikely integer
$paginate = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_text' => __('<i class="fas fa-angle-left"></i>'),
'next_text' => __('<i class="fas fa-angle-right"></i>'),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'type' => 'list'
) );
$result .= '<div class="blog-nav">' . str_replace( "<ul class='page-numbers'>", '<ul class="pagination">', $paginate ) . '</div>';
wp_reset_postdata();
return $result;
}
add_shortcode( 'paginated-list', 'shortcode_paginated_list' );
Any help would be greatly appreciated, thanks.
For anyone that might stumble upon this, this seems to be working as expected for some reason:
$the_query =
new WP_Query( array(
'post_type'=>'my_custom_post_type',
'posts_per_page' => '2',
'publish_status' => 'published',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
while ($the_query -> have_posts()) : $the_query -> the_post();
$result = 'fetching some stuff';
endwhile;
$big = 999999999; // need an unlikely integer
$paginate = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'prev_text' => __('<i class="fas fa-angle-left"></i>'),
'next_text' => __('<i class="fas fa-angle-right"></i>'),
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages,
'type' => 'list'
));
$result .= '<div class="blog-nav">' . str_replace( "<ul class='page-numbers'>", '<ul class="pagination">', $paginate ) . '</div>';
wp_reset_postdata();
return $result;

Wordpress posts loop pagination - first page return 125 posts instead of 10 and the rest return 10

I'm trying to show 10 posts per page with pagination on WordPress and the first page return 125 posts instead of 10 and the rest of the pages return 10 posts as requested, please assist :)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$output = array();
global $post;
$args = array('nopaging' => false, 'paged' => $paged, 'posts_per_page' => 10, 'post_type' => 'post', 'order'=> 'DES', 'orderby' => 'date');
$postslist = new WP_Query( $args );
if ( $postslist->have_posts() ) :
while ( $postslist->have_posts() ) : $postslist->the_post();
array_push($output, array("timestamp" => get_the_date('U'),"img_url" => get_the_post_thumbnail_url(), "title" => get_the_title(), "text" => get_the_content()));
endwhile;
wp_reset_postdata();
endif;
Here is the sample code which you can use for the pagination:
<?php
/**
* Looping through the content
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query (array(
'post_type' => 'custom_portfolio',
'orderby' => 'post_id',
'posts_per_page' => 10,
'paged' => $paged,
'order' => 'DESC'
)); ?>
<?php while ($loop -> have_posts()) : $loop -> the_post(); ?>
<?php endwhile; ?><?php wp_reset_query(); ?><?php wp_reset_postdata(); ?>
And here is the part for the pagination:
<!-- Pagination -->
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $loop->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Previous Page', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next Page', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
Here is a good start for you, just adopt as you want.
The sticky posts was the problem, I have excluded the sticky_posts from the query, and it fixed the problem
'ignore_sticky_posts' => 1

The pagination in wordpress not work correctly

I have a problem with pagination for the custom post. I used these code bellow for my site:
The content still show content on loop are correct but when I click next number, it still in first page without jumpping to the next page.
How to fix this issues ?
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'category_name' => 'nha-dat-binh-duong',
'posts_per_page' => 2,
'paged' => $paged
) );
?>
<?php if ( $query->have_posts() ) : ?>
<!-- begin loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
GET CONTENT HERE
<?php endwhile; ?>
<!-- end loop -->
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Tin mới nhất', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( '⏩', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>```
Have you refreshed your permalinks? Simply go to permalinks in WordPress and then refresh the page.

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>

Why is my pagination for custom post type not working?

Here is the code I'm using to display the pagination...
<?php
global $wp_query;
$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' => $wp_query->max_num_pages
) );
?>
But when you click next and got to /page/2/ it says "Page Not Found"
what am I doing wrong???
The code above is not working because it is not set for a custom post type.
If you look in the wordpress codex for paginate_links you will find your code under the
Basic Example
To add pagination to your search results and archives, you can use the following example
and is not working for your query because you have different query_vars, the code you should work with should be from the same codex page:
Example With a Custom Query
When querying a loop with new WP_Query set the 'total' parameter to the max_num_pages property of the WP_Query object.
with the query beeing:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 5,
'category_name' => 'gallery',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
?>
<!-- the loop etc.. -->
and the pagination code:
<?php
$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' => $the_query->max_num_pages
) );
?>
When working with pagination "Page not found" errors usually are caused by missuning the query vars.

Resources