首页 > 解决方案 > 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');

我的代码有问题吗?

标签: wordpressselectadvanced-custom-fields

解决方案


我不相信这将在您的 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');

看看这是否能让你朝着正确的方向前进。


推荐阅读