php - Pagination doesnt work in custom post type
Get the solution ↓↓↓Pagination template part includes common pagination function with style. The template part works forarchive.php
(it's for "single", you know default wp file) but doesn't work for custom post type.
Why not? How to solve it?
<?php get_header(); ?>
<main role="main">
<!-- section -->
<?php get_template_part( 'breadcrumb' );?>
<!-- Inner Pages Main Section -->
<section class="ulockd-service-details">
<div class="container">
<div class="col-md-12">
<div class="row">
<?php
/**
* Setup query to show the �services’ post type with �8’ posts.
* Output the title with an excerpt.
*/
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
if (have_posts()): while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php //if (have_posts()): while (have_posts()) : the_post(); ?>
<?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
?>
<div class="col-md-12 ulockd-mrgn1210">
<div class="ulockd-project-sm-thumb">
<img class="img-responsive img-whp" src="<?php printf( '%s', esc_url($image_src[0]) ); ?>" alt="">
</div>
</div>
<?php
}
?>
<div class="col-md-12 ulockd-mrgn1210">
<article class="ulockd-pd-content">
<div class="ulockd-bp-date">
<ul class="list-inline">
<li class="ulockd-bp-date-innner">On <a href="#"><span class="text-thm2"><?php the_time('j'); ?></span> / <?php the_time('F Y') ?></a></li>
<li class="ulockd-bp-comment"><a href="#"><span class="flaticon-nurse-head text-thm1"></span> <?php the_author_posts_link(); ?></a></li>
<li class="ulockd-bp-comment"><a href="#"><span class="flaticon-chat text-thm1"></span> <?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></a></li>
<li class="ulockd-bp-comment"><a href="#"><span class="flaticon-black-check-box text-thm1"></span> <?php the_category(); ?></a></li>
</ul>
</div>
<h3><?php the_title(); ?> </h3>
<p class="project-dp-one"><?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?></p>
<a class="btn btn-lg ulockd-btn-thm2" href="<?php the_permalink(); ?>"> Read More</a>
</article>
</div>
<?php get_template_part('pagination'); ?>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<?php endif; ?>
</div></div></div></section>
<?php get_footer(); ?>
</main>
Answer
Solution:
First of all, you don't need to include that template insidewhile
loop. That's wrong.
Then, if you want to have an archive page for yourteam
post type, you need to provide'has_archive' => true
withinregister_post_type()
function args.
Also, consider changing archive page default slug if needed. If you do so, you need to openSettings > Permalinks
for resetting your permalinks structure.
Then you could either use standardarchive.php
for the whole team archive page and standardtemplate-parts/content.php
for one post inside the loop or rewrite either of those by creatingarchive-team.php
orcontent-team.php
. Andthe_posts_pagination()
function will work on proper archive page (eitherarchive.php
orarchive-team.php
).
Answer
Solution:
I would try two things.
Add the argument paged to your wp_query args :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'team', 'post_status' => 'publish', 'paged' => $paged, 'posts_per_page' => 1, );
Put your pagination template outside the loop :
<?php endwhile; ?> <?php get_template_part('pagination'); ?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: git was not found in your path, skipping source download
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.