admin管理员组文章数量:1332628
I have a custom taxonomy term page set up to display posts associated with that term (taxonomy-insight_type-academia.php). I figured I could use the following to limit the posts on that page based on code found here:
/
add_action( 'pre_get_posts', function( $query) {
if ( $query->is_tax( 'academia', 'insight_type' ) ) {
$query->set( 'posts_per_page', 1 );
}
} );
I have added the term to the taxonomy but it doesn't seem to work. I can get it to work for the taxonomy, but not the term.
Is there a way to do this?
Thanks
I have a custom taxonomy term page set up to display posts associated with that term (taxonomy-insight_type-academia.php). I figured I could use the following to limit the posts on that page based on code found here:
https://developer.wordpress/reference/functions/is_tax/
add_action( 'pre_get_posts', function( $query) {
if ( $query->is_tax( 'academia', 'insight_type' ) ) {
$query->set( 'posts_per_page', 1 );
}
} );
I have added the term to the taxonomy but it doesn't seem to work. I can get it to work for the taxonomy, but not the term.
Is there a way to do this?
Thanks
Share Improve this question asked Jul 25, 2020 at 2:27 PhillPhill 1971 gold badge1 silver badge10 bronze badges1 Answer
Reset to default 0The taxonomy template you have (taxonomy-insight_type-academia.php
) is in the form of (taxonomy-{taxonomy}-{term}.php
), and the format for is_tax()
is is_tax( '<taxonomy>', '<term>' )
, so in your pre_get_posts
callback, you should:
// Use this.
if ( $query->is_tax( 'insight_type', 'academia' ) )
// Not this.
if ( $query->is_tax( 'academia', 'insight_type' ) )
Additionally — to avoid conflict with other queries, you should also check if the current query is the main one or not, and also check if it's an admin-side request.
if ( ! is_admin() && $query->is_main_query()
&& $query->is_tax( 'insight_type', 'academia' )
)
You may already know that, but I thought it'd be a good reminder. :)
本文标签: postsSetting postsperpage for taxonomy term template
版权声明:本文标题:posts - Setting posts_per_page for taxonomy term template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742233857a2437713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论