如何在 WP 短代码中显示 php 函数
How to display php function in WP shortcode
我应该说我才刚刚开始我的旅程 PHP 所以可能这就是为什么我不能做这么简单的任务。
我有一个简单的 php 代码,我需要将其用作简码。我在为短代码制作代码时没有问题,但我无法理解的是如何在其中插入 php 函数。我要插入的 php 函数是:
$( element ).airport([ 'moscow', 'berlin', 'stockholm' ]);
作为简码,我编写了这样的代码:
function home_words_shortcode() {
return $element .airport([ 'moscow', 'berlin', 'stockholm' ]);
}
add_shortcode( 'home-words', 'home_words_shortcode' );
我在网上进行了研究,发现我认为与我的代码相似的内容并尝试转换为环境,但似乎没有任何效果。
谁能帮帮我。
我不确定我是否理解你的意思,但这不是 php 函数:
$( element ).airport([ 'moscow', 'berlin', 'stockholm' ]);
如我所见,这是一个jquery代码,也许你选择器有问题,你可以得到所有的知识here. And a example
希望这对你有用。
PD:我不是以英语为母语的人,所以,请全面点:)
不确定为什么要将此脚本添加为短代码,但这可能是一种方式:
function home_words_shortcode( $atts ) {
$atts = shortcode_atts( array(
'element' => '#default-element',
'cities' => '',
), $atts, 'home-words' );
$cities = json_encode(array_filter(explode(',', $atts['cities'])));
return "
<script>
jQuery('{$atts['element']}').airport($cities);
</script>
";
}
add_shortcode( 'home-words', 'home_words_shortcode' );
它适用于像这样的简码:
[home-words element="#element" cities="moscow,berlin,stockholm"]
我应该说我才刚刚开始我的旅程 PHP 所以可能这就是为什么我不能做这么简单的任务。 我有一个简单的 php 代码,我需要将其用作简码。我在为短代码制作代码时没有问题,但我无法理解的是如何在其中插入 php 函数。我要插入的 php 函数是:
$( element ).airport([ 'moscow', 'berlin', 'stockholm' ]);
作为简码,我编写了这样的代码:
function home_words_shortcode() {
return $element .airport([ 'moscow', 'berlin', 'stockholm' ]);
}
add_shortcode( 'home-words', 'home_words_shortcode' );
我在网上进行了研究,发现我认为与我的代码相似的内容并尝试转换为环境,但似乎没有任何效果。 谁能帮帮我。
我不确定我是否理解你的意思,但这不是 php 函数:
$( element ).airport([ 'moscow', 'berlin', 'stockholm' ]);
如我所见,这是一个jquery代码,也许你选择器有问题,你可以得到所有的知识here. And a example
希望这对你有用。
PD:我不是以英语为母语的人,所以,请全面点:)
不确定为什么要将此脚本添加为短代码,但这可能是一种方式:
function home_words_shortcode( $atts ) {
$atts = shortcode_atts( array(
'element' => '#default-element',
'cities' => '',
), $atts, 'home-words' );
$cities = json_encode(array_filter(explode(',', $atts['cities'])));
return "
<script>
jQuery('{$atts['element']}').airport($cities);
</script>
";
}
add_shortcode( 'home-words', 'home_words_shortcode' );
它适用于像这样的简码:
[home-words element="#element" cities="moscow,berlin,stockholm"]