Adding pagination to custom post loop in page - wordpress

I have created a custom page template (testimonials-page.php) and in that template I am
loading custom post type 'testimonials' using the following loop:
<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
'orderby' => 'post_date',
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
How do I add pagination to that? I installed the WP Paging plugin, and while that plugin works
great when I call the pagination into category.php using:
<p><?php wp_paging(); ?></p>
Inserting the same thing into testimonial-page.php results in broken formatting and links that
404 on me.

Firstly, never EVER use query_posts unless your intention is to modify the default Wordpress Loop.
Instead, switch to WP Query.
Here's something I wrote for a theme I did for a client using all built-in Wordpress functions. It's been working pretty well for me so far, so I'll integrate it into your code as best as I can:
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'post_type' => 'testimonials',
'orderby' => 'post_date',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
echo '
<div id="wp_pagination">
<a class="first page button" href="'.get_pagenum_link(1).'">«</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '
<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
</div>
';
wp_reset_postdata();
endif;
?>
Jan 2018 Edit:
Also consider using paginate_links, since it's also built into Wordpress, and has more robust options and capabilities.

Try this code for custom loop with pagination:
<?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' => get_option('posts_per_page'),
'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(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?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;
?>
Source:
WordPress custom loop with pagination

Related

Wordpress Pagination not working - Page url changes but content doesn't change

Homepage displays all the post and I want pagination for this. After clicking on the pagination link page number changes in url but same content is shown on all pages, also the pagination link which number shows only 1 selected. Like if i selcted pagination link 3, url shown page/3, but page number is 1 and content doesnt even change.
<?php
/**
* Genesis Sample.
*
* This file adds the landing page template to the Genesis Sample Theme.
*
* Template Name: Home
*
* #package Genesis Sample
* #author StudioPress
* #license GPL-2.0-or-later
* #link https://www.studiopress.com/
*/
get_header();
$paged = (isset($_GET['paged'])) ? intval($_GET['paged']) : 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : $paged;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => $paged,
);
$wp_query = new WP_Query($args);
?>
<div class="body-container">
<div class="blogs-banner">
<?php
$image = get_field('image');
if (!empty($image)) : ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" class="img-main" />
<?php endif; ?>
</div>
<div class="about-container">
<div class="blog-list">
<div class="row-same-height" id="postContainer" data-type="post" data-count="<?php echo $wp_query->found_posts; ?>">
<?php
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<a href="" class="link-post">
<div class="col-same-height">
<div class="content">
<div class="TopArticleCard-Container">
<div class="img-container">
<?php
$thumbnail = get_post_meta(get_the_id(), 'large_img', true);
if ( $thumbnail ) { ?>
<img src="<?php echo $thumbnail; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" loading="lazy"/>
<?php } else if ( has_post_thumbnail() ) {
the_post_thumbnail('large', ['title' => get_the_title()], ['alt' => get_the_title()]); ?>
<?php
} else { ?>
<img src="<?php echo get_site_url(); ?>/wp-content/uploads/2020/09/default.jpg" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" loading="lazy"/>
<?php } ?>
</div>
<div class="article-description">
<div class="article-tag">
<?php $cat = get_the_category(); echo $cat[0]->cat_name; ?>
</div>
<div class="content">
<div class="article-title">
<?php the_title(); ?>
</div>
</div>
</div>
</div>
</div>
</div>
</a>
<?php endwhile;
// post_pagination($query);
endif;
wp_reset_postdata();
?>
</div>
<nav class="pagination-nav">
<ul class="pagination">
<?php
if ( $wp_query->have_posts() ) :
$currentPage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$pagination_args = array(
'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
'format' => '/page/%#%',
'current' => $currentPage,
'total' => $wp_query->max_num_pages,
'prev_text' => 'Prev',
'next_text' => 'Next',
);
echo paginate_links(array_merge($pagination_args, array('query' => $wp_query)));
endif;
?>
</ul>
</nav>
</div>
</div>
</div>
</div>
<?php get_footer();
It seems that the issue you are facing is related to the query for fetching the posts and the pagination links. Here are a few things you can try to fix this issue:
Check if the paged parameter is set correctly in the URL: In the code you provided, you are using $_GET['paged'] to get the current page number, but in the pagination links, you are using get_query_var('paged'). Make sure that the URL of the pagination links contains the paged parameter, for example, http://example.com/page/3/?paged=3.
Make sure to use the correct query object for pagination links: In the code you provided, you are using $wp_query to generate the pagination links. However, this is not the correct query object to use if you are using a custom WP_Query. Instead, you should use the query object for the custom WP_Query, which is $query in this case.
Here's the updated code with these changes:
$paged = (isset($_GET['paged'])) ? intval($_GET['paged']) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => $paged,
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
// Display the post content
endwhile;
endif;
// Generate pagination links
if ($query->max_num_pages > 1) :
$current_page = max(1, get_query_var('paged'));
echo '<div class="pagination">';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $query->max_num_pages,
));
echo '</div>';
endif;
wp_reset_postdata();

Pagination link on static page

The manual pagination is working when I change the page directly in the url. But it's not working with the links, the link is always the page 2.
It's displayed on a static page. I changed get_query_var('paged') to get_query_var('page') to make the pagination (manual) working.
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'paged' => ( get_query_var('page') ? get_query_var('page') : 1)
);
query_posts($args);
while (have_posts()) : the_post();
?>
<?php echo get_permalink(); ?>
<?php endwhile; ?>
<?php previous_posts_link( 'Older Posts' ); ?>
<?php next_posts_link( 'Newer Posts' ); ?>
You should use, new WP_Query($args) An example;
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'paged' => ( get_query_var('page') ? get_query_var('page') : 1)
);
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
More details : https://codex.wordpress.org/Class_Reference/WP_Query

How to re-use initial page content loop in WordPress?

I'm working on a site whereby there is a simple template for a certain page in the site.
The page in question is an About Us page.
On the page there is the first main WordPress loop which just spits out the standard content of the main Wysiwyg field.
Underneath that the template has some code which spits out a loop of a custom post type called Staff. This custom post type has information on each staff member and each one is spat out there.
What I have added to the page via the backend is an Advanced Custom Field using ACF Pro. The field is called Our Story (our_story) and I'd like to be able to spit that field out on the template underneath all the staff members.
If I place the following code :
<p><?php the_field('our_story') ?></p>
into the template in the main WordPress loop which is at the start of the template then it shows up fine but I effectively want to start the same loop again but underneath the loops which have spat out the staff members.
If I copy the exact same loop which is at the start of the template to the end of the template then I don't get anything output on the page where that code is. Do I therefore need to reset the code or something or is there an easy way of perhaps caching field content and placing it elsewhere in the template?
Sorry entire template code shown here :
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php the_field('our_story', '710'); ?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
I've adjusted your template to include named WP_Query() loops, and apply wp_reset_postdata() after each custom loop. This should allow the final the_field() call to have access to the current post ID, which is what Advanced custom fields needs in order to fetch the post metadata for the current post.
Behind the scenes, ACF runs (int) get_the_ID(); to get the ID of the current post. If your custom query loops are not reset, this won't work.
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
// Specify the custom query, so you can build a custom loop.
// ---------------------------------------------------------
$staff_query = new WP_Query( $args );
?>
<?php if ($staff_query->have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while ($staff_query->have_posts()): ?>
<?php
// Set up the post to allow use of template tags
// -------------------------------------------------
$staff_query->the_post();
?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$expert_query = new WP_Query( $args );
?>
<?php if ($expert_query->have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while ($expert_query->have_posts()): ?>
<?php
// Set up post
// -------------------------------------------------
$expert_query->the_post();
?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php
// this should work now:
the_field('our_story');
//the_field('our_story', '710');
?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
I think it's easy to get confused with custom loop logic when it's all handled within a template - I prefer to manage post data for custom loops by means of a function or class method which returns an array of post data - you can then loop over this in your template.
Hope this helps.
Resources: https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
it's because you need a post id to get the field. By default it is the current post's id.
try this: <p><?php the_field('our_story', xxx) ?></p> where xxx is the post id of About Us.

wordpress 404 after click on pagination links

i have a problem with the pagination in wordpress. I'm having a category-template wich involkes a query for a custom posts. The problem is that when i add a pagination and you go to the next page the pagination add $_GET['paged'] but the template reject it and trow 404 page. I also discovered that if $_GET['paged']=1 everything works fine but if it is 2 or more throw 404 page. The thing is that i try in the index loop and it also does not work. I'm using html5blank theme.
Here is the code to the query:
<section id="inner-pad">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
//'posts_per_page' => 6,
'post_type' => 'menu-pub',
'cat' =>3,
'paged' => $paged,
'order'=>'DESC'
);
$wp_query = new WP_Query( $args );
$i=1;
while ( $wp_query->have_posts() )
{
$wp_query->the_post();
?>
<article class="article-first-vision <?= ($i==2)?'mar':'' ?>" >
<?php if ( has_post_thumbnail()) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail('menu-pub'); ?>
</a>
<?php
}; ?>
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php the_content(); ?>
</p>
</article>
<?php
if($i==3){$i=0;}enter code here
$i++;
}
?>
</section>
<?php
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
?>
Here is a new dialog where i put a theme for this:
http://wordpress.org/support/topic/custom-post-and-category-template-pagination-problem?replies=2#post-4560385
try to add global $wp_query before paginate_links()
Hi here is the answer to my question:
add_action( 'parse_query','changept' );
function changept() {
if( is_category() && !is_admin() )
set_query_var( 'post_type', array( 'menu-pub', 'new-pub', 'read-pub', 'awards-pub', 'live-pub','post' ));
return;
}

pagination on custom post wordpress

<?php query_posts('showposts=5&post_type=html5-blank'); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" class="clearfix" <?php post_class(); ?>>
//Loop Here
<!-- /Article -->
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
I get a Page doesn't exist error on my pagination link.
Result link is: www.mywebsite.com/blog/page/2/
This is a blog page. I have edited the loop code.
HELP.........
I encountered similar problem for my homepage (index.php) which list the posts. I keep getting a page not found. The instruction in https://codex.wordpress.org/Pagination got www.domain.com/page/2/ working for me.
First remove the query_posts part from the template files (index.php, category.php)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
query_posts($args);
Then add the below in your functions.php
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it's the main query
if (!is_admin() && $query->is_main_query()){
// alter the query for the home and category pages
if(is_home()){
$query->set('posts_per_page', 3);
}
if(is_category()){
$query->set('posts_per_page', 3);
}
}
]
add_action( 'pre_get_posts', 'my_post_queries' );
Note: Both HTML5 Blank Theme and Underscore Theme gave me 404 errors for pagination. The above solution got pagination working for both themes.
I rather would use WP_Query and use the paged pagination parameter. Read more about this here: WP_Query#Pagination_Parameters
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'html5-blank',
'posts_per_page' => 5,
'paged'=>$paged
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" class="clearfix" <?php post_class(); ?>>
//Loop Here
<!-- /Article -->
<?php endwhile; endif; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
Please let me know :)
Second example:
global $post;
global $paged, $wp_query;
$args = array( 'posts_per_page' => 5, 'post_type' => 'html5-blank', 'paged' => $paged );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post);
// loop
the_title(); // or what it is needed inside the loop
endforeach;
if ( $wp_query->max_num_pages > 1 ) :
previous_posts_link('« Newer');
next_posts_link('Older »');
endif;
<?php
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'html5-blank', //Post type
'posts_per_page' => 5, //How many post u want to display per page
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<img src="<?=$url?>" width="350" height="350" class="thumbnail imageRight"/>
<h1><?php the_title(); ?></h1>
<p><?php the_excerpt(); ?></p>
<?php } } ?>
<div class="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
) );
?>
</div>
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'YOUR_POST_TYPE_NAME',
'paged' => $paged,
'posts_per_page' => 5)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
// YOUR CODE
endwhile;
echo '<nav>';
echo '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
Would you please try above code?

Resources