gpt4 book ai didi

wordpress - 仅限自定义帖子类型的无限 posts_per_page

转载 作者:行者123 更新时间:2023-12-05 08:58:58 26 4
gpt4 key购买 nike

我需要在存档循环中显示所有现有帖子,以显示自定义帖子类型“车辆”。

这是我目前所拥有的:

function get_all_vehicle_posts( $query ) {
$query->set( 'posts_per_page', '-1' );
}
add_action( 'pre_get_posts', 'get_all_vehicle_posts' );

而且我看到了我想要的无限帖子。但是我需要将此更改限制为我的自定义帖子类型。

我试过:

    if ( 'vehicle' == $query->post_type ) {
$query->set( 'posts_per_page', '-1' );
}

但这似乎行不通。我想我们在查询运行之前不知道帖子类型,除非它是查询的特定参数?

如何将此功能限制为特定的帖子类型?

最佳答案

使用帖子类型的 is_post_type_archive 函数检查您的预获取帖子。

您需要检查查询是否不是管理查询以避免影响管理区域,以及检查查询是否是主查询。

function get_all_vehicle_posts( $query ) {
if( !is_admin() && $query->is_main_query() && is_post_type_archive( 'vehicle' ) ) {
$query->set( 'posts_per_page', '-1' );
}
}
add_action( 'pre_get_posts', 'get_all_vehicle_posts' );

http://codex.wordpress.org/Function_Reference/is_admin

http://codex.wordpress.org/Function_Reference/is_main_query

http://codex.wordpress.org/Function_Reference/is_post_type_archive

关于wordpress - 仅限自定义帖子类型的无限 posts_per_page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18968916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com