wordpress - ACF 动态选择值不显示数据
问题描述
这个问题让我疯狂了将近 2 周。我知道我不是 Wordpress 专家,所以我在这里寻求帮助。
我创建了一个 href,当用户单击它时,它将转到新页面。
<a href="./create-class?post=<?php echo $post->ID; ?>">Add Class2</a>
此 href 发布帖子 ID。网址显示:
[ http://localhost/dev6/create-class/?post=289][1]
创建类页面:
在创建类页面,我使用 GET 方法从 url 显示帖子 ID
$post = $_GET['post'];
我在创建类页面中有用于创建新帖子的 acf 表单。在此表单中,有动态选择字段,但选择字段不显示任何数据。
<?php acf_form(array(
'post_id' => 'new_post',
'field_groups' => array(150),
'post_title' => false,
'post_content' => false,
'new_post' => array(
'post_type' => 'classes',
'post_status' => 'publish',
),
'return' => '%post_url%',
'submit_value' => 'Submit',
//'updated_message' => 'Course Submit!',
)); ?>
在我的function.php中,我为动态选择创建了函数:
function acf_load_t_first_name2_field_choices($field) {
global $post;
//$post = $_GET['post'];
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = get_field('t_first_name',$post->ID);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $choice ] = $choice;
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=t_first_name2', 'acf_load_t_first_name2_field_choices');
我的代码有问题吗?
解决方案
我不相信这将在您的 create-class 模板中起作用:
$post = $_GET['post'];
您需要在 functions.php 文件中设置类似的内容:
function custom_query_vars_filter($vars) {
$vars[] .= 'post';
return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );
然后,在您的 create-class 模板中,您可以从 URL 中获取变量,如下所示:
$post = get_query_var('post');
看看这是否能让你朝着正确的方向前进。
推荐阅读
- c# - 实例变量与 getter/setter
- python - Ansible 无法从 MACOS(源机器)ping Windows 主机
- javascript - 从子类访问父类中的状态
- vba - 如何从 Access 数据集创建 2D 可视化
- r - 2 R 中数据框字段中的数值
- javascript - concat src img 与角度条件
- reactjs - 注入自定义 React 组件
- python - 在保持表格数据自然顺序的同时,无需替换的随机样本
- excel - 将数据从一个工作簿更新到另一个工作簿的 VBA 代码
- matlab - Matlab:从包含坐标的txt文件中的数据创建矩阵