get_field()WordPress上的PHP错误

get_field()WordPress上的PHP错误,php,wordpress,Php,Wordpress,我正在努力掌握WordPress的开发。我对PHP有基本的了解,但我知道在WordPress中开发时语法略有不同 场景: 为了确保我的PHP代码正常工作,我运行了以下测试: //定义查询 $args=array('post_type'=>'案例研究'); $thew_query=newwp_query($args); //环路 如果($the_query->have_posts()){//check,则查询是否有posts? 回声“”; while($the\u query->have\u p

我正在努力掌握WordPress的开发。我对PHP有基本的了解,但我知道在WordPress中开发时语法略有不同

场景

为了确保我的PHP代码正常工作,我运行了以下测试:

//定义查询
$args=array('post_type'=>'案例研究');
$thew_query=newwp_query($args);
//环路
如果($the_query->have_posts()){//check,则查询是否有posts?
回声“
    ”; while($the\u query->have\u posts()){ $the_query->the_post(); 回显“
  • ”。获取标题()。
  • ;//吐出页面标题 } 回声“
”; /*恢复原始Post数据*/ wp_reset_postdata(); }否则{ //没有找到帖子 }
试试这个

您需要在循环中传递post in
get\u字段。否则它将获得当前页面id

if ( $the_query->have_posts() ) { 
    $PostId = get_the_ID();
    $case_study_heading = get_field('case_study_heading', $PostId);
    $case_study_subheading = get_field('case_study_subheading', $PostId);
    $case_study_backgroung = get_field('case_study_background', $PostId);
    $case_study_play = get_field('case_study_play_button', $PostId);

    echo $case_study_heading;
} else {
    echo "no posts";
}
如果这是你的代码

if ( $the_query->have_posts() ) { 
    $case_study_heading = get_field('case_study_heading');
    // etc (for brevity)

    echo $case_study_heading;
} else {
    echo "no posts";
}
那么您还没有包括循环,因此您的
get_field()
s将全部寻址包含的帖子/页面,而不是循环中的帖子

因此,您还需要包括以下循环:

if ( $the_query->have_posts() ) { 
    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        $case_study_heading = get_field('case_study_heading');
        // etc (for brevity)

        echo $case_study_heading;
    }
} else {
    echo "no posts";
}

PHP变量不能有破折号,只能有下划线$my_var有效,但$my var无效not@Willi啊,是啊,我现在看不出有什么错误。。。但我在前端没有看到任何结果?我更新了我的问题来解释。为解决一个问题干杯!