I tried to search right solution for this but I cannot find where I went wrong with code..I want to display pagination on my custom page which uses WP_Query...here is the code:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$frontpageNews = new WP_Query(array(
'category_name' => 'recent-news',
'posts_per_page' => 3,
'paged' => $paged,
'offset' => 1
));
while($frontpageNews->have_posts()) {
$frontpageNews->the_post(); ?>
the_title();
}
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
My posts are showing but not pagination...Sorry if there is existing topic on this, I just couldnt find one that it helped this case, so I came here to ask.
Thanks in advance for your time.
You can get pagination using following code.
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $frontpageNews->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', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
This is working for me so it should work for you.
Related
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.
I have problem with pagination on a static front-page I have coded. This is not working setting to homepage:
<?php if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
}elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
}else {
$paged = 1;
}
$query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 36,
'category_name' => $cat_slug[3],
'paged' => $paged
));
while ($query->have_posts()) : $query->the_post(); ?>
how to fix them?
You can try as follows -
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
}elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
}else {
$paged = 1;
}
$query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 36,
'category_name' => $cat_slug[3],
'paged' => $paged
));
while ($query->have_posts()) : $query->the_post();
// Print your post data
endwhile;
// Paginations
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', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
wp_reset_postdata();
Add this code after your while loop
<div class="nav-links">
<div class="nav-previous"><?php echo get_next_posts_link( __( 'Older Entries', 'yourtextdomain' ), $query->max_num_pages ); ?></div>
<div class="nav-next"><?php echo get_previous_posts_link( __( 'Newer Entries', 'yourtextdomain' ) ); ?></div>
</div>
You can get Older Entries and Newer Entries via this.
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
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.
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>