如何在WP快捷代码中显示php函数


How to display php function in WP shortcode

我应该说我只是从PHP开始我的旅程,所以这可能就是我不能做这么简单任务的原因。我有一个简单的php代码,我需要使用它作为快捷代码。我在为shortcode编写代码时没有遇到任何问题,但我不明白如何在里面插入php函数。我试图插入的php函数是:

$(  element  ).airport([ 'moscow', 'berlin', 'stockholm' ]);

作为一个短代码,我制作了一个这样的代码:

    function home_words_shortcode() {

    return  $element .airport([ 'moscow', 'berlin', 'stockholm' ]);
}
add_shortcode( 'home-words', 'home_words_shortcode' );

我在网上做了一项研究,发现了我认为与我的代码相似的东西,并试图转换到环境中,但似乎什么都不起作用。有人能帮我吗。

不确定为什么要将此脚本添加为短代码,但这可能是一种方法:

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函数:

$(  element  ).airport([ 'moscow', 'berlin', 'stockholm' ]);

正如我所看到的,这是一个jquery代码,也许你在选择器方面有问题,你可以在这里获得所有的知识。还有一个的例子

我希望这对你有用。

PD:我不是以英语为母语的人,所以请综合:)