Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress WP-包含html和php混合的短代码(ACF变量)_Wordpress_Advanced Custom Fields_Shortcode - Fatal编程技术网

Wordpress WP-包含html和php混合的短代码(ACF变量)

Wordpress WP-包含html和php混合的短代码(ACF变量),wordpress,advanced-custom-fields,shortcode,Wordpress,Advanced Custom Fields,Shortcode,我正在尝试创建一个包含html和php的WP短代码。例如,类似的东西不起作用: function my_first_shortcode() { $content = <<<EOT <h1>Some title</h1> <p><?php the_field('description'); ?></p> EOT; return $content; } add_shortcode('my_sho

我正在尝试创建一个包含html和php的WP短代码。例如,类似的东西不起作用:

function my_first_shortcode() {
    $content = <<<EOT
    <h1>Some title</h1>
    <p><?php the_field('description'); ?></p>
EOT;
return $content; 
} 
add_shortcode('my_shortcode', 'my_first_shortcode');
_字段'name_of_field';通常输出指定变量/字段的内容高级自定义字段

这样做正确吗?如果是,我会怎么做?如果我能将变量传递给短代码,那也太好了


谢谢

我总是喜欢对我的短代码使用输出缓冲,如下例

function my_first_shortcode() {
    ob_start(); ?>
    <h1>Some title</h1>
    <p><?php echo the_field('description'); ?></p>
    <?php
    return ob_get_contents();
} 
add_shortcode('my_shortcode', 'my_first_shortcode');

首先,不能在herdoc中编写PHP标记。 您可以这样使用它:

$the_field = 'the_field';
$content = <<<EOT
<h1>Some title</h1>
<p>{$the_field('description')}</p>
EOT;

嗯。它完成了这项工作,但它在一个页面上的两个位置输出:在主要内容之后的底部,这是我放置短代码的地方,在标题之后的右上方。这与上面的小功能无关,它输出的另一个原因一定是我认为它可能有。在谷歌搜索之后,我将ob_get_内容改为ob_get_clean,然后它修复了它。我真的不明白它做了什么,但它起了作用
function my_first_shortcode($atts)
{
    $att_1 = $atts['att_1'];
    $att_2 = $atts['att_2'];
}
add_shortcode('my_shortcode', 'my_first_shortcode');
add_shortcode('location_start_your_application_group', 'start_your_application_group');

function start_your_application_group() {
    ob_start();

    $start_your_application_group = '';
    $start_your_application_group .= '<section class="start-your-application">';
     if ( have_rows( 'start_your_application_group', 'option' ) ) : 
         while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row(); 
           $heading = get_sub_field( 'heading' );
             $content = get_sub_field( 'content' );

             if ( $heading !== '' ) {
                    $start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
             }
             if ( $content !== '' ) {
                $start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
             }

             $image = get_sub_field( 'image' );
             if ( $image ) { 
                $start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . esc_url($image['url']) .'" alt="' . $image['alt'] . '" /></div>';
            } 
        endwhile;
    endif;

    $start_your_application_group .= '</section>';
    $start_your_application_group = ob_get_clean();

    return $start_your_application_group;
}