admin管理员组文章数量:1279014
I have several custom post types (created through CPT UI plugin). I want to display posts from these post types on my page, for example "News". I've created a page and inserted in it shortcode [show_posts post_type="novyny"].
I've got a plugin that processes this shortcode
function foo($args){
$post_type = $args['post_type'];
$custom_post_type = new WP_Query(
array(
'post_type' => $post_type,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 4
)
);
if( $custom_post_type->have_posts() ) :
while( $custom_post_type->have_posts() ): $custom_post_type->the_post(); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>
<p> <?php the_modified_date(); echo ", "; the_modified_time() ?></p>
<p> <?php the_excerpt() ?></p>
<?php endwhile;
the_posts_pagination();
endif;
wp_reset_postdata();
}
add_shortcode('show_posts', 'foo');
Posts are displayed, but pagination blog doesn't appear at the page even if there is more than 4 posts to display. What's wrong?
I have several custom post types (created through CPT UI plugin). I want to display posts from these post types on my page, for example "News". I've created a page and inserted in it shortcode [show_posts post_type="novyny"].
I've got a plugin that processes this shortcode
function foo($args){
$post_type = $args['post_type'];
$custom_post_type = new WP_Query(
array(
'post_type' => $post_type,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 4
)
);
if( $custom_post_type->have_posts() ) :
while( $custom_post_type->have_posts() ): $custom_post_type->the_post(); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>
<p> <?php the_modified_date(); echo ", "; the_modified_time() ?></p>
<p> <?php the_excerpt() ?></p>
<?php endwhile;
the_posts_pagination();
endif;
wp_reset_postdata();
}
add_shortcode('show_posts', 'foo');
Posts are displayed, but pagination blog doesn't appear at the page even if there is more than 4 posts to display. What's wrong?
Share Improve this question asked Feb 5, 2016 at 16:36 VlodkoVlodko 1931 silver badge13 bronze badges 1- pagination functions operate on the main query, you have a custom query. you're also not passing any pagination parameters to the query in the shortcode, so even if you were to visit other pages, you'd get the same results. I suggest searching around here for paginating custom queries, there are no shortage of questions and answers on the subject. – Milo Commented Feb 5, 2016 at 17:04
3 Answers
Reset to default 3the_posts_pagination
uses global
query and global query does not have pagination. So it is the correct behavior of WordPress.
To overcome from this problem assign custom query to global query then after looping again restore the global query.
function foo($args){
$post_type = $args['post_type'];
global $wp_query;
$original_query = $wp_query;
$custom_post_type = new WP_Query(
array(
'post_type' => $post_type,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 4
)
);
$wp_query = $custom_post_type;
if( $custom_post_type->have_posts() ) :
while( $custom_post_type->have_posts() ): $custom_post_type->the_post(); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>
<p> <?php the_modified_date(); echo ", "; the_modified_time() ?></p>
<p> <?php the_excerpt() ?></p>
<?php endwhile;
the_posts_pagination();
endif;
wp_reset_postdata();
$wp_query = $original_query;
}
Thank you all, guys. I've solved it this way:
- added
$paged
variable:$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
(for main page you should useget_query_var('page')
, for other pages -$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
) - And added a new argument for WP_Query:
'paged' => $paged
My code now:
function foo($args){
$post_type = $args['post_type'];
/* Added $paged variable */
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
global $wp_query;
$original_query = $wp_query;
$query_args = array(
'post_type' => $post_type,
'posts_per_page' => 3,
/* And added a new argument for WP_Query */
'paged' => $paged
);
$custom_post_type = new WP_Query( $query_args );
$wp_query = $custom_post_type;
if ( $custom_post_type->have_posts() ) :
while ( $custom_post_type->have_posts() ) :
$custom_post_type->the_post(); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>
<p> <?php the_modified_date(); echo ", "; the_modified_time() ?></p>
<p> <?php the_excerpt() ?></p>
<?php endwhile;
the_posts_pagination();
endif;
wp_reset_postdata();
$wp_query = $original_query;
}
A small update to Vlad's last post. You need to change
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
to
$paged = ( $original_query->query['paged'] ) ? $original_query->query['paged'] : 1;
本文标签: Pagination in plugin with custom post type
版权声明:本文标题:Pagination in plugin with custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741237909a2363388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论