gpt4 book ai didi

php - 如何将 css 类添加到我的示例 php 代码中

转载 作者:行者123 更新时间:2023-11-28 16:56:59 26 4
gpt4 key购买 nike

我想在这段 php 代码的输出中添加一个类

function acf_esg_tax_field_loc( $atts, $content = null ) {
$a = shortcode_atts(array('post_id' => "1"), $atts);
$term = get_field( "field_56cb3d84cf32a", $a['post_id'] );

echo $term->name;
}

add_shortcode( 'acfesgtax_loc', 'acf_esg_tax_field_loc' );

该代码在我的 Wordpress 函数 php 中用于生成短代码。

应该是这样的

function acf_esg_tax_field_loc( $atts, $content = null ) {
$a = shortcode_atts(array('post_id' => "1"), $atts);
$term = get_field( "field_56cb3d84cf32a", $a['post_id'] );

echo '<div class="OutputClass" >';
echo $term->name;
echo '</div>';
}

add_shortcode( 'acfesgtax_loc', 'acf_esg_tax_field_loc' );

不幸的是,这不起作用。

你能帮忙吗?

最佳答案

作为WordPress Code Reference州。

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

所以首先将其从 echo 更改为 one return。您可以定义一个变量,连接到它并最终返回它。

结果:

function acf_esg_tax_field_loc( $atts, $content = null ) {
$a = shortcode_atts(array('post_id' => "1"), $atts);
$term = get_field( "field_56cb3d84cf32a", $a['post_id'] );

$sR = '<div class="OutputClass" >';
$sR .= $term->name;
$sR .= '</div>';
return $sR;
}

add_shortcode( 'acfesgtax_loc', 'acf_esg_tax_field_loc' );

关于php - 如何将 css 类添加到我的示例 php 代码中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56134811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com