Wordpress shortcode pagination - wordpress

I've created a shortcode for custom post with advanced custom fields. All works correctly except the pagination. I've tried all of the options I can see on other posts, but none work for me. Pagination is working on custom post pages.
function link_carstwo( $atts ) {
extract(shortcode_atts(array(
'cartype' => 'porsche',
'section' => 'make'
), $atts));
$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
) );
$list = ' ';
echo '<div id="car-container">
<ul id="carlist">';
//Setup the query to retrieve the posts that exist under each term
global $post;
$posts = new WP_Query (array(
'post_type' => 'cars',
'orderby' => 'menu_order',
'order' => 'ASC',
$section => $cartype,
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => $paged,
));
// Here's the second, nested foreach loop that cycles through the posts associated with this category
while ( $posts->have_posts() ) { $posts->the_post();
////set up post data for use in the loop (enables the_title(), etc without specifying a post ID--as referenced in the stackoverflow link above)
$price = get_field('price', $post->ID);
$car_image = get_field('car_image', $post->ID);
$image_position = get_field('image_position', $post->ID);
$make = get_field('make', $post->ID);
$year = get_field('year', $post->ID);
$date_purchased = get_field('date_purchased', $post->ID);
$finance_type = get_field('finance_type', $post->ID);
$job_title = get_field('job_title', $post->ID);
$model = get_field('model', $post->ID);
$list .= '<li class="carbox">
<p>TEST</p>
<div class="image2" style="background-image:url(' . $car_image .');background-position: ' . $image_position . ' center;"></div>
<p class="car"> '.$make.' ' . $model . ' ' . $year . ' </br> £ ' . $price . ' ' . $date_purchased . '</p>
<p class="finance">' . $finance_type . '</p>
<p class="fademeSmall"> ' . $job_title . '</p>
<p class="linked"></p>
</li>';
}
'</ul></div>';
return
'<div class="navigation centerWidth">'
. $list
.the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => __( '<', 'textdomain' ),
'next_text' => __( '>', 'textdomain' ),
) )
. '<div class="nav-previous">' . get_next_posts_link( __( '<span class="meta-nav">←</span> Older posts' ) ) . '</div>'
. '<div class="nav-next">' . get_previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>' ) ) . '</div>'
. 'TEST</div>' .
wp_reset_query();
}
add_shortcode( 'car-gridtwo', 'link_carstwo' );

Try this for your pagination
<?php
global $wp_query;
$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' => $wp_query->max_num_pages
) );
?>

The way you are using wp_query and the global variables is extremely convoluted and wrong.
while ( $post->have_posts() ) { $posts->the_post();
On the while condition you should have $posts->have_posts() rather than $post->have_posts()
You also don't need the setup_postdata($post);
I'm not sure that this will solve the issue, everything else seems "fine" to me.
EDIT
Having a closer look at the code I just noticed that your return statement is within the loop, that's wrong and might be the reason why you are having the pagination issue. Just close the While block before the return statement (and obviously remove the last closing bracket, before wp_reset_query). Don't forget to do the change I suggested before as well.

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;

Multiple post query with pagination in shortcode

I want to multiple query with pagination of post type POST. I write code but it don't return right post when click for 2nd page or 3rd page. May be pagination is working properly or other errors. I want to multiple query with pagination of post type POST. I write code but it don't return right post when click for 2nd page or 3rd page. May be pagination is working properly or other errors. I can't find it.
function blogpost_shortcode($atts, $content = null){
extract( shortcode_atts( array(
'count' => '1',
), $atts) );
$page = get_query_var('page');
$q1 = new WP_Query( array(
'posts_per_page' => $count,
'post_type' => 'post',
'paged' => get_query_var('page')
) );
$q1_ids = wp_list_pluck( $q1->posts, 'ID' );
$q2 = new WP_Query(array('posts_per_page' => $count, 'post_type' => 'post', 'paged' => get_query_var('page'), 'post__not_in' => $q1_ids));
$blog_markup = '';
$blog_markup .= '
<div class="bwog_blog_wrap">';
while($q1->have_posts()) : $q1->the_post();
$idd = get_the_ID();
$blog_markup .= '
<div class="single_blog_wrap">
<h3>'.get_the_title($idd).'</h3>
</div>
';
endwhile;
while($q2->have_posts()) : $q2->the_post();
$idd = get_the_ID();
$blog_markup .= '
<div class="single_blog_wrap">
<h3>'.get_the_title($idd).'</h3>
</div>
';
endwhile;
$total_pages = $q1->max_num_pages;
$big = 999999999;
if ($total_pages > 1){
$current_page = max(1, get_query_var('page'));
$blog_markup.= '<nav class="page-nav">';
$blog_markup.= paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => 'Prev',
'next_text' => 'Next'
));
$blog_markup.= '</nav>';
}
$blog_markup .= '</div>';
wp_reset_query();
return $blog_markup;
}
add_shortcode('bwog_blog', 'blogpost_shortcode');
It looks like you are checking for the query variable page, but in the pagination code you are setting the format to ?paged=%#%. You'll want to make sure you are using the same query variable everywhere, so you should set the format to ?page=%#%.
Also, unrelated to the answer but something I noticed, you set the variable $page to get_query_var('page'), but then never use it. I would recommend using it everywhere you have get_query_var('page') to make it your code more efficient and maintainable.

Pagination Issue on Wordpress

There are plenty of questions regarding pagination... I've poured through them, and put together the following code (see below). The problem: Some category pages aren't showing any results (a category with only 3 posts). Other's aren't showing all of the posts in that category (one category has 80 posts, but are only showing 60)...
Below is my code...
On my category.php page...
<?php
$currCat = get_category(get_query_var('cat'));
$cat_name = $currCat->name;
$cat_id = get_cat_ID( $cat_name ); // Get cat ID
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'meta_key' => 'ratings_average', // WP-Rating Plugin Rating Value
'orderby' => 'meta_value_num',
'order' => 'DESC',
'cat' => $cat_id,
'posts_per_page' => 10,
'paged' => $paged,
);
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
get_template_part( 'entry' );
endwhile;
//Pagination
post_pagination();
else:
?>Sorry, no results at this time.<?php
endif;
?>
and on my functions.php page...
if ( ! function_exists( 'post_pagination' ) ) :
function post_pagination() {
global $wp_query;
$pager = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;
You don't have to WP query the category posts because your using the category.php file.
Category.php Loop =
<?php if(have_posts()):
while(have_posts()): the_post();
get_template_part( 'entry' );
endwhile;
wp_reset_postdata();
?>
<?php if ( function_exists('post_pagination') ) { post_pagination(); } else if ( is_paged() ) { ?>
<ul class="pagination">
<li class="older"><?php next_posts_link('<i class="glyphicon glyphicon-arrow-left"></i> ' . __('Previous', 'theme')) ?></li>
<li class="newer"><?php previous_posts_link(__('Next', 'theme') . ' <i class="glyphicon glyphicon-arrow-right"></i>') ?></li>
</ul>
<?php } ?>
Then your pagination function:
if ( ! function_exists( 'post_pagination' ) ) {
function post_pagination() {
global $wp_query;
$big = 999999999; // This needs to be an unlikely integer
$paginate_links = paginate_links( array(
'base' => str_replace( $big, '%#%', 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' => __('<i class="fa fa-angle-double-left" aria-hidden="true"></i>'),
'next_text' => __('<i class="fa fa-angle-double-right" aria-hidden="true"></i>'),
'type' => 'list'
) );
$paginate_links = str_replace( "<ul class='page-numbers'>", "<ul class='pagination'>", $paginate_links );
$paginate_links = str_replace( "<li><span class='page-numbers current'>", "<li class='active'><a href='#'>", $paginate_links );
$paginate_links = str_replace( "</span>", "</a>", $paginate_links );
$paginate_links = preg_replace( "/\s*page-numbers/", "", $paginate_links );
// Display the pagination if more than one page is found
if ( $paginate_links ) {
echo $paginate_links;
}
}
}
The post limit will come from what you set in your reading settings, hope it helps

Add WordPress custom taxonomy terms as css class

On a custom post type archive I'm trying to add the custom taxonomy terms slug as a css class to the tag. I've managed to get it to output the page->ID but struggling to get the $term->slug to work. Feel like I'm missing something really simple. Here's the full code, thanks for any help:
<?php
$parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'archive', 'orderby' => 'menu_order' , 'order' => 'ASC', 'sort_column' => 'menu_order' ) );
foreach ( $parent_pages as $parent_page ) {
echo '<h1 class="page-heading" id="';
echo $parent_page->post_name;
echo '">';
echo $parent_page->post_title;
echo '</h1>';
echo '<div class="wrapper grid4">';
$all_pages = get_pages(array( 'post_type'=> 'archive', 'orderby' => 'menu_order' , 'order' => 'ASC', 'sort_column' => 'menu_order' ) );
$child_pages = get_page_children($parent_page->ID, $all_pages );
foreach ( $child_pages as $child_page ) {
echo '<article class="post col ' . $child_page->ID, $term->slug .'">';
echo '<a class="fancybox" data-fancybox-type="iframe" href="http://www.iofpi.co.uk/civicworks.net/wp-content/plugins/pdfjs-viewer-shortcode/web/viewer.php?file=http://www.iofpi.co.uk/civicworks.net/wp-content/uploads/2014/05/Citizen_Manchester.pdf" title="' . the_title_attribute('echo=0') . '" >';
echo get_the_post_thumbnail( $child_page->ID, 'medium');
echo '</a>';
echo '<h1>';
echo $child_page->post_title;
echo '</h1>';
echo '</article>';
}
echo '</div>';
}
?>
Use this function get_the_terms( $id, $taxonomy ) just pass it the post id in your case '$parent_page->ID' and the Name of taxonomy to retrieve terms from. For example: 'category', 'post_tag', 'taxonomy slug'
It will return an object and you can then access the slug with, say $result->slug.

Wordpress WP_User_Query Pagination

I have a short snippet of code that queries the users of the website based on meta values. The code already works fine and perfect. Now, my only problem here is that I can't seem to figure out how to make the pagination work just like when you use pagination on worpdress posts. Your help means a lot to us. Thank you in advance and more power to stackoverflow
Here's the code that needs to have a pagination:
<ul id="ulfriends">
<?php
// The User Query
$user_query = new WP_User_Query( $args = array (
'number' => 10,
'meta_query' => array(
array(
'key' => 'sponsor',
'value' => $current_user->user_nicename,
'compare' => '=',
'type' => 'CHAR',
),
),
'count_total' => true,
) );
echo count($user_query->results);
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<li>' . $user->first_name . ' ' . $user->last_name . '<br>' . $user->date_activation . '<br>' . get_avatar( $user->user_email, 165 ) . '</li>' ;
}
} else {
echo 'No users found.';
}
?>
</ul>
try this code
<ul id="ulfriends">
<?php
$no=12;// total no of author to display
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if($paged==1){
$offset=0;
}else {
$offset= ($paged-1)*$no;
}
$user_query = new WP_User_Query( array( 'role' => 'Subscriber', 'number' => $no, 'offset' => $offset ) );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<li>' . $user->first_name . ' ' . $user->last_name . '<br>' . $user->date_activation . '<br>' . get_avatar( $user->user_email, 165 ) . '</li>' ;
}
} else {
echo 'No users found.';
}
?>
</ul>
<?php
$total_user = $user_query->total_users;
$total_pages=ceil($total_user/$no);
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $paged,
'total' => $total_pages,
'prev_text' => 'Previous',
'next_text' => 'Next',
'type' => 'list',
));
?>

Resources