我正在尝试使用 Divi 博客模块中的高级服装字段 (ACF) 插件在服装帖子类型 (CPT) 上实现显示/隐藏功能。 我不想只显示那些在名为“show”的 ACF 字段中设置了 True 值的 CPT。
我正在使用
主题:迪维
帖子中自定义字段的插件:ACF - 高级自定义字段
我创建了Divi儿童主题(CT)。 我通过将自定义帖子类型 (CPT) 放在 CT 的functions.php 中来注册它。 我安装并激活了 WP Engine 的高级自定义字段 (ACF)。 我创建了包含 3 个服装字段的字段组,并且成功显示了其中两个字段的值(文本字段和日期选择器) 到这里一切都好。
现在的问题是: 我创建了第三个 ACF,名为“show”。它是 True/False 字段。它用于标记当前 CPT 是否应显示在前端。 我正在 Divi 的博客模块中显示所有 CPT,该模块在“高级”下有条件,但找不到根据“显示”字段中的值进行显示的选项。
在寻找解决方案时,我发现了一个建议,将以下代码添加到CT的functions.php中:
function filter_CPT_by_custom_field( $query ) {
// Check if we are on the frontend and the main query
if ( ! is_admin() && $query->is_main_query() ) {
// Check if the current post type is the New Custom Post Type
if ( $query->get( 'post_type' ) === 'new_CPT' ) { // Make sure to replace 'new_CPT' with the actual slug of your New Custom Post Type
// Filter posts by the custom field value
$meta_query = array(
array(
'key' => 'show',
'value' => 1, // Set this to the value of 'Yes' in your yes/no field
'compare' => '=',
),
);
// Modify the query to include the custom field filter
$query->set( 'meta_query', $meta_query );
}
}
}
add_action( 'pre_get_posts', 'filter_CPT_by_custom_field' );
那不行。
有人对如何在 Divi 博客模块中使用 ACF 实现 CPT 上的显示/隐藏功能有任何建议吗?还有其他更好的方法吗?