繁体   English   中英

如何在简码中插入 PHP 代码?

[英]How to insert a PHP CODE inside shortcode?

我想插入一个php代码,但是放置时出错

主题生成的简码

[col_grid span="4" span__sm="14" height="1-2" visibility="show-for-medium"]

[ux_banner height="500px" bg="***[banner-picture]***" bg_size="original"]

[text_box width="100" scale="148" position_x="50" position_y="100" bg="rgb(88, 32, 123)"]

[ux_text text_color="rgb(247, 128, 44)" class="uppercase"]

<p><strong>preencha a proposta de adesão</strong></p>
[/ux_text]

[/text_box]

[/ux_banner]

[/col_grid]

我的 PHP 代码

add_action('foto_banner', 10 );
  
function foto_banner() { ?>
<?php if(get_field('foto_banner')) { ?>


<?php the_field('foto_banner'); ?>

<?php }else{
    echo "Texto não informado";
}
}

add_shortcode( 'banner-picture', 'foto_banner');

您不能将 PHP 函数放在简码中。

但是,您不需要这样做。 您可以使用您的 foreach 循环创建 html 并将其存储在变量中,例如 $shortcodehtml。

所以,之后,你可以打电话

echo do_shortcode('[O_U user_name="operator" blocked_message="This page is restricted for guests."]' . $shortcodehtml . '[/O_U]');

更好的版本是,如果您在简码 function 中创建 output - 从我的角度来看,没有必要将整个 html 传递给简码 - 但它应该可以正常工作。

你可以这样写你的简码:

<?php

function bannerPicture(){
    ob_start();

    if( get_field( 'foto_banner' ) ) {
        the_field( 'foto_banner' );
    } else {
        echo "Texto não informado";
    }

    $output = ob_get_contents();
    ob_end_clean();
    return $output;
    
}
add_shortcode( 'banner-picture', 'bannerPicture' );

?>

如果您从主题选项页面获取,请确保在get_field()第二个参数或option中添加当前页面 ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM