gpt4 book ai didi

php - 在 CMB2 选项中添加帖子类别

转载 作者:搜寻专家 更新时间:2023-10-31 21:02:13 25 4
gpt4 key购买 nike

我需要在我的 CMB2 WordPress 插件的下拉选项中添加特定类型的帖子。我已经找到在我的代码中生成我的字段的位置;然而,我看到 taxonomies 被用作过滤器,但我需要一个帖子 post_type

使用分类法

$cmb_events->add_field( array (
'name' => __('Catégorie d\'événement','aqmat'),
'id' => $prefix . 'categorie',
'type' => 'taxonomy_select',
'taxonomy' => 'aqmat_categorie_evenement'
));

我想我会查询我的帖子以仅获取我想要的帖子,并使用逻辑作为 taxonomy 字段将它们添加到下拉列表中

我尝试获取我的帖子类型

// Get posts in desired category
$posts = array();
$args = array(
'post_type' => 'post_type_i_want',
'orderby' => 'id',
'posts_per_page' => -1
);
$getPosts = get_posts($args);
foreach($getPosts as $post) {
setup_postdata($post);
// Yes, I am aware I would *technically* have several times the same value
// Not getting there yet, so not an issue for me right now
// Besides, my category currently has but 1 post
array_push($getPosts, array(
'standard' => __( 'Option value', 'cmb2' )
));
}
// Add field
$cmb_events->add_field( array (
'name' => __('Label','cmb2'),
'id' => $prefix . 'enseignant',
'type' => 'taxonomy_select',
'options' => $teamMembers
));

但是,我没有收到任何错误,下拉列表中也没有任何值。在官方Documentation中查询帖子似乎得不到多少,所以我对这个问题一无所知。有什么建议吗?


编辑

I have posted an answer使用 Advanced Custom Fields .


编辑 #2

Another answer已提供指向正确页面的链接。

最佳答案

您似乎正在将 $teamMembers 分配给选项,但它未定义且未分配任何值。

// Get posts in desired category
$posts = array();
$args = array(
'post_type' => 'post_type_i_want',
'orderby' => 'id',
'posts_per_page' => -1
);
$getPosts = get_posts($args);
foreach($getPosts as $post) {
setup_postdata($post);
// Yes, I am aware I would *technically* have several times the same value
// Not getting there yet, so not an issue for me right now
// Besides, my category currently has but 1 post
array_push($posts, array(
'standard' => __( 'Option value', 'cmb2' )
));
}
// Add field
$cmb_events->add_field( array (
'name' => __('Label','cmb2'),
'id' => $prefix . 'enseignant',
'type' => 'taxonomy_select',
'options' => $posts
));

试试这段代码,希望你能得到选项值

关于php - 在 CMB2 选项中添加帖子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39729020/

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