WordPress:为另一个短代码中的值调用短代码

问题描述 投票:0回答:2

我对此很陌生,所以如果这是一个愚蠢的问题,请对我轻松一点。

我正在使用事件管理器构建站点,并为我的WordPress插件添加地理位置。我希望用户能够输入他们的自动填充位置(通过GMW),并使日历在EM输出中仅显示距该位置一定距离的事件。我已经(手持)到了一个短代码,可以吐出所输入位置的坐标。 EM完整日历简码具有一个称为“ near”的属性,该属性获取坐标并随后输出所需的日历。

此刻的代码:

[fullcalendar near=[gmw_current_latlng] near_distance=20]

[[gmw_current_latlng]通常返回经度和经逗号分隔的长整数。通常,near at需要50.111123,-45.234,等等。

我的问题是,这种顽固的方法似乎无法获得想要的东西。再说一次,我对编码还很陌生,并不了解很多,但是我已经在这个问题上研究了好几个星期了,却没有找到答案。我尝试了许多不同的路线,但是这种方式使我非常接近我想去的地方。

GMW开发人员对这个问题说了这句话:

“问题是,我不确定您是否可以将值传递给使用另一个简码的简码。我从来没有尝试过的最好的方法是使用过滤器和自定义函数“注入”直接协调日历功能。“

[如果他是对的并且不可能,我不知道如何执行他的第二个建议。希望我能解决这个问题,因为坦率地说,我的网站取决于它的正常运行。

html wordpress shortcode geocode
2个回答
2
投票

正如@Jeppe所述,您可以执行Nested Shortcodes

[shortcode_1][shortcode_2][/shortcode_1]

但是解析器不喜欢将短码值作为其他短码的属性。

听起来您似乎依赖于一些插件及其短代码,所以我不建议您编辑这些短代码-但是如果您查看Shortcode API,添加自己的代码很容易。为简单起见,此示例将不包含确保短代码存在/已安装插件等的“适当”方法,而仅假设它们存在。

// Register a new shortcode called [calendar_with_latlng]
add_shortcode( 'calendar_with_latlng', 'calendar_with_latlng_function' );

// Create the function that handles that shortcode
function calendar_with_latlng_function( $atts ){
    // Handle default values and extract them to variables
    extract( shortcode_atts( array(
        'near_distance' => 20
    ), $atts, 'calendar_with_latlng' ) );

    // Get the current latlng from the gmw shortcode
    $gmw_current_latlng = do_shortcode( '[gmw_current_latlng]' );

    // Drop that value into the fullcalendar shortcode, and add the default (or passed) distance as well.
    return do_shortcode( '[fullcalendar near='. $gmw_current_latlng .' near_distance='. $near_distance .']' );
}

提供的[gmw_current_latlng]返回您的[fullcalendar]短代码的可用格式,您现在应该可以使用结合了这两个新的短代码:[calendar_with_latlng],或者还可以添加near_distance属性:[calendar_with_latlng near_distance=44]

您只需要将以上功能放入functions.php中,创建一个Simple Plugin,或将它们保存到文件中并将其添加到Must-Use Plugins目录中。


0
投票

当然,您可以将简码作为另一个简码的属性来传递。唯一的问题是,属性不通过[或]。因此,您已将其括起来替换为html条目。

[替换[,用]替换],应该没问题。这是一个例子。

function foo_shortcode( $atts ) {

    $a = shortcode_atts( array(
        'foo' => "Something",
        'bar' => '',
    ), $atts );

    $barContent = html_entity_decode( $atts['bar'] );
    $barShortCodeOutput = do_shortcode($barContent);

    return sprintf("Foo = %s and bar = %s", $a['foo'], $barShortCodeOutput);
}
add_shortcode( 'foo', 'foo_shortcode' );


function bar_shortcode( $atts ) {
    return "Output from bar shortcode";
}
add_shortcode( 'bar', 'bar_shortcode' );

然后将其放在您的编辑器上

[foo bar=[bar] ]

请参阅我们将短代码[bar]作为[foo]的属性传递。所以输出应该是-Foo = Something and bar = Output from bar shortcode

我知道它看起来有点讨厌,但是可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.