使用return时,变量出现在php中的html之上

时间:2017-03-29 01:22:32

标签: php wordpress

我正在尝试将变量$about放在我要返回的html中...但是当我查看输出时,它会将$about放在html代码之上......

function cc_shortcode( $atts ) {

 $other_page = 498;
 $about = the_field('cc_about_this_course', $other_page);

    extract( shortcode_atts( array(
        'id' => '',
    ), $atts) );
    $result = '<div class="main">
<div class="accordion">
<div class="accordion-section">

<a class="accordion-section-title active" href="#accordion-1">ABOUT THIS COURSE<i class="fa fa-arrow-down"></i></a>
<div id="accordion-1" class="accordion-section-content open" style="display: block;">
<p>' . $about . '</p>
</div>
</div>
';

    return $result;


}

1 个答案:

答案 0 :(得分:1)

https://www.advancedcustomfields.com/resources/the_field/

the_field用于显示当前帖子中显示值的原因..

使用get_field的解决方案; https://www.advancedcustomfields.com/resources/get_field/

如果没有get_field ..(对于常见问题)......就像这样..

你可以ob_start();和ob_get_clean为你解决问题

function cc_shortcode( $atts ) {

     $other_page = 498;

ob_start();
the_field('cc_about_this_course', $other_page);
$about = ob_get_clean();


        extract( shortcode_atts( array(
            'id' => '',
        ), $atts) );
        $result = '<div class="main">
    <div class="accordion">
    <div class="accordion-section">

    <a class="accordion-section-title active" href="#accordion-1">ABOUT THIS COURSE<i class="fa fa-arrow-down"></i></a>
    <div id="accordion-1" class="accordion-section-content open" style="display: block;">
    <p>' . $about . '</p>
    </div>
    </div>
    ';

        return $result;


    }