admin管理员组文章数量:1122846
I made a query for my custom post type "websites". I'm trying to get the pagination working but I'm running in some errors. I think the whole code is ok, only the part just before the end while tags. I just don't know how to get this right. Any help would be enormously appreciated!
<?php
// WP_Query arguments
$args = array (
'post_type' => 'website',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => '5',
'posts_per_archive_page' => '5',
'ignore_sticky_posts' => false,
'order' => 'DESC',
);
// Get current page and append to custom query parameters array
$mfs_query['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// The Query
$mfs_query = new WP_Query( $args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $mfs_query;
// The Loop
if ( $mfs_query->have_posts() ) {
while ( $mfs_query->have_posts() ) {
$mfs_query->the_post();
// do something
?> <div class="submission">
<a class="submission-thumb" href="<?php echo get_permalink(get_the_ID()); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
<a class="submission-title" href="<?php echo get_permalink(get_the_ID()); ?>">
<h3><?php the_title(); ?></h3> <span class="posted-ago"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></span>
</a>
<a class="submission-link" href="<?php the_field('site_url'); ?>" target="_blank">Visit</a>
</div>
<?php } ?>
<?php endwhile; else : ?>
<?php wp_reset_postdata(); ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php // Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $mfs_query->max_num_pages ); ?>
<?php $wp_query = null; $wp_query = $temp_query;?>
?>
I made a query for my custom post type "websites". I'm trying to get the pagination working but I'm running in some errors. I think the whole code is ok, only the part just before the end while tags. I just don't know how to get this right. Any help would be enormously appreciated!
<?php
// WP_Query arguments
$args = array (
'post_type' => 'website',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => '5',
'posts_per_archive_page' => '5',
'ignore_sticky_posts' => false,
'order' => 'DESC',
);
// Get current page and append to custom query parameters array
$mfs_query['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// The Query
$mfs_query = new WP_Query( $args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $mfs_query;
// The Loop
if ( $mfs_query->have_posts() ) {
while ( $mfs_query->have_posts() ) {
$mfs_query->the_post();
// do something
?> <div class="submission">
<a class="submission-thumb" href="<?php echo get_permalink(get_the_ID()); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
<a class="submission-title" href="<?php echo get_permalink(get_the_ID()); ?>">
<h3><?php the_title(); ?></h3> <span class="posted-ago"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></span>
</a>
<a class="submission-link" href="<?php the_field('site_url'); ?>" target="_blank">Visit</a>
</div>
<?php } ?>
<?php endwhile; else : ?>
<?php wp_reset_postdata(); ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php // Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $mfs_query->max_num_pages ); ?>
<?php $wp_query = null; $wp_query = $temp_query;?>
?>
Share
Improve this question
asked Mar 7, 2015 at 0:19
fabrikgrafikfabrikgrafik
31 silver badge6 bronze badges
1
|
1 Answer
Reset to default 0Just make sure that the code you have write in archive-{post_type}.php
file or you can use ajax pagination
if you need to use page template:
Add this 'has_archive' => 'page template'
when you register the custom post type
本文标签: loopPagination in custom post type page template
版权声明:本文标题:loop - Pagination in custom post type page template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289833a1928388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pre_get_posts
to modify it so that it only shows 5 per page, and you can change the URL of the archive using therewrite
parameter when registering the CPT. Replacing the main query with a whole new query is super wasteful and causes lots of problems ( usually pagination but others too ) – Tom J Nowell ♦ Commented Aug 15, 2020 at 21:21