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();
Related
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 );
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();
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>
I trying to add pagination to my WordPress website, it's working fine with blog page, but with custom category when click any link of pagination URL change, but content still the same.
Custom category code
<?php query_posts('category_name=sport');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('loop'); ?>
<?php endwhile; // конец цикла
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
Pagintion in function.php
function pagination() {
global $wp_query;
$big = 999999999;
$links = paginate_links(array(
'base' => str_replace($big,'%#%',esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'type' => 'array',
'prev_text' => 'Назад',
'next_text' => 'Вперед',
'total' => $wp_query->max_num_pages,
'show_all' => false,
'end_size' => 4,
'mid_size' => 4,
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
));
if( is_array( $links ) ) {
echo '<ul class="pagination">';
foreach ( $links as $link ) {
if ( strpos( $link, 'current' ) !== false ) echo "<li class='active'>$link</li>";
else echo "<li>$link</li>";
}
echo '</ul>';
}
}
Please help
instead of: query_posts('category_name=sport');
try this:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'category_name' => 'sport',
'posts_per_page' => 6,
'paged' => $page
);
query_posts($args);
For further customization check the List of Query Vars
Try this:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'category_name' => 'sport'
);
$the_query = new WP_Query( $args );
?>
Hope this helps :)
Please check below code:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'work',
'categories'=>'sports',
'posts_per_page' => 2,
'paged' => $page,
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile;
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
You have to mention post type also as per the example
Hope this work for you.
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