Following advice on Wordpress Pagination in a Shortcode I'm working on a shortcode for wordpress and trying to add pagination to it. I'm almost there, just don't know why wordpress isn't updating paginated pages content, e.g. if I click on "Next" or "1", "2", "3" etc I always see the same page.
This is my code:
function carforyou_LatestCar($atts){
ob_start();?>
<div class="row">
<?php
extract( shortcode_atts(array('show' =>''), $atts ));
extract( shortcode_atts(array('brand' =>''), $atts ));
$loop = new WP_Query( array(
'post_type' => 'auto',
'auto-brand' => $brand,
'posts_per_page'=>$show,
'paged' => $paged,
'offset' => 0
));
while ($loop->have_posts()) : $loop->the_post();
global $paged; ?>
<div class="col-list-3">
<div class="featured-car-list">
<div class="featured-car-img">
<a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
<?php if(has_post_thumbnail()):
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;
?>
</a>
<?php carforyou_AutoType(); ?>
<div class="compare_item">
<div class="checkbox">
<button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
</div>
</div>
</div>
<div class="featured-car-content">
<h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
<div class="price_info">
<?php if(!empty($post->DREAM_auto_price)): ?>
<p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
<?php endif; ?>
<div class="car-location">
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?>
<span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
<?php endif; ?>
</div>
</div>
<ul>
<?php carforyou_featuredList(); ?>
</ul>
</div>
</div>
</div>
<?php endwhile;
$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' => $loop->max_num_pages
) );
wp_reset_query();
?>
</div>
<?php }
Any advice? :)
Thank you!
add in functions.php
// Numbered Pagination
if ( !function_exists( 'kris_pagination' ) ) {
function kris_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_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,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
shortcode
<?php kris_pagination(); ?>
Related
I have a custom post type in WordPress and on the archive page, I want to add pagination.
I have the following code, but when I click on page 2 or 'next', it reloads page 1.
My first thought was it should be added before wp_reset_postdata(). But that displayed the links alongside every post.
What am I missing, please?
<!-- custom query for village life posts -->
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 6,
'post_type' => 'villagelife',
'orderby' => 'post_date',
'order' => 'ASC',
'paged' => $paged,
);
$villagelifeArchive = new WP_Query( $args );
while ( $villagelifeArchive->have_posts() ) {
$villagelifeArchive->the_post();
?>
<div class="grid-item">
<div class="grid-item-wrapper">
<div class="grid-item-container">
<?php if ( has_post_thumbnail() ) : ?>
<div class="grid-image-top">
<span class="centered project-image-bg"><a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'archive-village-life-image' ); ?></a>
</span>
</div>
<?php endif; ?>
<div class="grid-item-content">
<span class="item-title"><?php the_title(); ?></span>
<span class="item-category"><?php echo get_the_term_list( $post->ID, 'village-life-category', '', ', ', ' ' ); ?></span>
<span class="item-excerpt"><?php echo wp_trim_words( get_the_content(), 18 ); ?></span>
<?php echo '<a class="button ast-button more-info" href=' . get_the_permalink() . '>Read More..</a>'; ?>
</div>
</div>
</div>
</div>
<?php
}
wp_reset_postdata();
$total_pages = $villagelifeArchive->max_num_pages;
print_r( $total_pages );// give me 2 ..the correct answer.
if ( $total_pages > 1 ) {
$current_page = max( 1, get_query_var( 'paged' ) );
?>
<div>
<?php
echo paginate_links(
array(
'base' => get_pagenum_link( 1 ) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
)
);
?>
</div>
<?php
}
?>
I am currently unable to help with your issue, but I have got the php I use for pagination.
Add the below to your functions.php file
<?php
function og_pagination() {
global $wp_query;
$big = 999999999;
$links = paginate_links(
array(
'base' => str_replace( $big, '%#%', html_entity_decode( get_pagenum_link( $big ) ) ),
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'mid_size' => 5,
'prev_next' => true,
'prev_text' => '<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="14px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M9.7,18.3L9.7,18.3c0.39-0.39,0.39-1.02,0-1.41L5.83,13H21c0.55,0,1-0.45,1-1v0c0-0.55-0.45-1-1-1H5.83l3.88-3.88 c0.39-0.39,0.39-1.02,0-1.41l0,0c-0.39-0.39-1.02-0.39-1.41,0L2.7,11.3c-0.39,0.39-0.39,1.02,0,1.41l5.59,5.59 C8.68,18.68,9.32,18.68,9.7,18.3z"/></svg>',
'next_text' => '<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="14px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M14.29,5.71L14.29,5.71c-0.39,0.39-0.39,1.02,0,1.41L18.17,11H3c-0.55,0-1,0.45-1,1v0c0,0.55,0.45,1,1,1h15.18l-3.88,3.88 c-0.39,0.39-0.39,1.02,0,1.41l0,0c0.39,0.39,1.02,0.39,1.41,0l5.59-5.59c0.39-0.39,0.39-1.02,0-1.41L15.7,5.71 C15.32,5.32,14.68,5.32,14.29,5.71z"/></svg>',
'type' => 'array',
)
);
if ( $links ) {
echo '<div class="pagination">';
echo '<ul>';
foreach ( $links as $link ) {
printf( '<li>%s</li>', str_replace( '/page/1', '', $link ) );
}
echo '</ul>';
echo '</div>';
}
}
My pagination function shows in my index page , but when call function in a custom Template page (page-news.php) it don't show !!
functions.php
function numbering_pagination() {
global $wp_query;
$all_pages = $wp_query->max_num_pages;
$current_page = max(1,get_query_var('paged'));
if ($all_pages >1) {
return paginate_links(array(
'base' => get_pagenum_link() . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'mid_size' => 3,
'end_size' => 3,
'prev_text' => 'السابق',
'next_text' =>'التالي'
));
}
}
page-news.php
<?php /* Template Name: news */
get_header(); ?>
<div id="fh5co-blog-section" class="fh5co-section-gray">
<div class="container">
<div>
<div class="text-center heading-section animate-box">
<h3>news</h3>
</div>
</div>
</div>
<div class="container">
<div class="row row-bottom-padded-md">
<?php
$args = array(
'post_type' => 'post',
'category_name'=> 'news'
);
$posts = new WP_Query( $args );
while( $posts->have_posts() ):
$posts->the_post();
?>
<div class="col-lg-4 col-md-4 col-sm-6">
<div class="fh5co-blog animate-box">
<img src="<?php the_post_thumbnail_url('full')?>" alt="" />
<div class="blog-text">
<div class="prod-title">
<h3><?php the_title()?></h3>
<span class="posted_by"><?php the_time('F jS, Y'); ?></span>
<p><?php the_content('<span class="read-more"> ... more</span>'); ?></p>
</div>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
</div>
</div>
<!-- ------ pagination ------ -->
<div class="pagination-numbers text-center">
<?php echo numbering_pagination() ?>
</div>
<!-- ------ END News ------ -->
How can I solve this?
From the WordPress codex:
<?php
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( 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:
<?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
) );
?>
Mind the:
'total' => $the_query->max_num_pages
Also see: https://codex.wordpress.org/Function_Reference/paginate_links
I know there have been so many pagination questions, but I'm really stumped here. I have a "gallery" hierarchical custom post type. For the parent posts, I'm using an index template, and the child posts uses more typical single page code.
Here's some of the single-gallery.php file. I just check to see if the post has children. If it does, I want it to be an index page.
get_header();
$template = get_template_directory_uri();
//If it's an index page, redirect it
$args = array(
'post_parent' => $post->ID,
'post_type' => 'gallery',
'numberposts' => -1,
'post_status' => 'publish'
);
$children = get_children( $args );
if ($children){
get_template_part('template-parts/content', 'gallery-index');
get_footer();
} else{
After that else, it outputs all the code for the single display, which is working fine.
In content-gallery-index.php, I have.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
Then it goes on to handle code for another, unrelated page. Lower in the template, I have the following (I included all of it by request):
<?php elseif (is_page(343) || $post_type == 'gallery' ):
$post_parent = 343;
$type = 'page';
if ($post_type == 'gallery'){
$post_parent = $post->ID;
$type = 'gallery';
}
$args = array(
'post_type' => $type,
'post_parent' => $post_parent,
'posts_per_page' => 6,
'paged' => $paged,
);
$query = new WP_Query($args);
if ($query->have_posts()): ?>
<div class="box-steps">
<?php while($query->have_posts()): $query->the_post();
$image = get_the_post_thumbnail();
$permalink = get_the_permalink();
if (!get_field('dont_include_in_services')): ?>
<section class="box-step">
<div class="box-step-img-wrapper">
<a href="<?php echo $permalink; ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<div class="box-step-content-wrapper">
<h2 class="box-step-title our-services-title header-font"><?php echo get_the_title(); ?></h2>
<?php if ($post_type == 'gallery'): ?>
<p class="box-step-content"><?php echo get_the_content($post->ID); ?></p>
<?php else: ?>
<p class="box-step-content"><?php echo esc_html(get_field('preview_text')); ?></p>
<a class="box-step-link block bold" href="<?php echo $permalink; ?>">
READ MORE
</a>
<?php endif; ?>
</div>
</section>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php endif; wp_reset_postdata(); ?>
<!-- pagination here -->
<?php
custom_pagination($query->max_num_pages,"",$paged);
?>
<?php endif; ?>
functions.php
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$prev_arrow = is_rtl() ? '»' : '«';
$next_arrow = is_rtl() ? '«' : '»';
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'list',
'add_args' => false,
'add_fragment' => '',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
//echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}
The strange thing is that the numbered pagination is output fine, but when I click on page two, it's like the URL just gets rewritten and the page just refreshes. This same code has worked on another page, but that page is not a custom post type. I've also tried the trick of turning wp_query to null and resetting it, but then I get other errors.
I'm building a custom page template and I can't seem to get the pagination to work. It keeps showing the same posts as on the home/first page. I have tried a lot of different code but getting the same problem with each results.
This is the current query I am using:
<?php
global $paged;
global $wp_query;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=2&post_type=post'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="news-item">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
?>
<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php
$wp_query = null;
$wp_query = $temp;
?>
Found the solution, this code worked:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => '2',
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="news-item">
<?php
if ( has_post_thumbnail() ) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
echo '<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>';
}
else {
}
;?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
<div id="single-post-container"></div>
<a class="button-1 load-more" href="<?php echo get_permalink(); ?>">Lees meer</a>
<a class="ajax-close button-1" href="#">X</a>
</div>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Try this code just copy pase
<?php
$args = array(
'post_type'=> 'post',
//'category_name' => 'blog',
'orderby' => 'post_date',
//'posts_per_page'=>'1'
'paged' => get_query_var('paged')
);
query_posts( $args );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php // Wordpress Pagination
$big = 999999999; // need an unlikely integer
$links = 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,
'prev_text' => '<',
'next_text' => '>',
'type' => 'array'
) );
if(!empty($links)){ ?>
<ul class="pagination">
<?php
foreach($links as $link){
?>
<li><?php echo $link; ?></li>
<?php
}
wp_reset_query(); ?>
</ul>
<?php } ?>
i have following page where i filter posts based on their category. This filter works fine, but if there are multiple pages of filtered results and i click to go to page 2 the filter resets and shows every post again instead of page 2 of the filtered results. How can i go to the second page of the filtered results?
Code:
<form class="form-inline" method="POST" action="/intranet/bibliotheek/">
<?php $intranetCategorie = get_terms( 'intranet_categorie');
if (!empty($intranetCategorie) && !is_wp_error($intranetCategorie )) {
echo '<select class="form-control training-drop" name="intranetCategorie">';
echo '<option value="empty">Alle Items</option>';
foreach ($intranetCategorie as $terms) {
if(0 != $terms->parent ){
echo '<option value="'. $terms->slug .'">' .$terms->name.'</option>';
}
}
echo '</select>';
}
?>
<input class="btn btn-intra" type="submit" value="Filter Resultaten">
</form>
<?php foreach ($intranetCategorie as $terms) {
$categorie[] = $terms->slug;
}
if(isset($_POST['intranetCategorie']) && $_POST['intranetCategorie'] != 'empty'):
$categorie = $_POST['intranetCategorie'];
endif;
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'intranet',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'intranet_categorie',
'field' => 'slug',
'terms' => 'bibliotheek',
),
array(
'taxonomy' => 'intranet_categorie',
'field' => 'slug',
'terms' => $categorie,
),
),
'posts_per_page' => 5,
'paged' => $paged
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3 class="intranet-title"><?php the_title(); ?></h3>
<p class="post-info">Geplaatst op <?php the_time('j F Y'); ?></p>
<p><?php the_content(); ?></p>
<?php
$file = get_field('upload');
if (!empty($file)) {
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
// icon
$icon = $file['icon'];
if( $file['type'] == 'image' ) {
$icon = $file['sizes']['thumbnail'];
}
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank">
<img src="<?php echo $icon; ?>" />
<span><?php echo $title; ?></span>
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
<?php } ?>
<?php if ( 'post' == get_post_type() ) : ?>
<footer class="edit">
<?php edit_post_link( __( 'Bewerk', 'soml' ), '<span class="glyphicon glyphicon-pencil"> ', '</span>' ); ?>
</footer><!-- .entry-footer -->
<?php else : ?>
<?php edit_post_link( __( 'Bewerk', 'soml' ), '<footer class="entry-footer"><span class="glyphicon glyphicon-pencil"> ', '</span></footer><!-- .entry-footer -->' ); ?>
<?php endif; ?>
<hr>
<?php endwhile; ?>
<div class="pagination-links-intranet">
<?php
$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' => $the_query->max_num_pages,
'prev_text' => '« Vorige',
'next_text' => 'Volgende »'
) );
?>
</div>
<?php else : ?>
<?php echo'<p>Er zijn geen berichten gevonden</p>' ?>
<?php endif; ?>
Pagination after i filter on a category (working as intended):
Pagination after i select page 2 of the filtered results:
The filter resets and will show all posts again. It should go to the second page of the filtered results.
Switching $_POST to $_GET will fix the problem since $_POST doesn't carry over when switching pages.