【问题标题】:Shortcode Attributes not being passed to handler function短代码属性未传递给处理函数
【发布时间】:2013-09-13 04:50:13
【问题描述】:

不确定是什么,但短代码中的属性没有被传递给处理函数。

Wordpress 帖子中的短代码

[k_recipe recipe="snack"]Here is some content[/k_recipe]

简码函数

add_shortcode("k_recipe", "recipe_shortcode");

function recipe_shortcode($attributes, $content){

    return "Hi " . $attributes["recipe"] . " Hi " . $content;
}

短代码输出

Hi  Hi Here is some content

为什么没有传递零食值?有什么线索吗??

【问题讨论】:

  • 所以我重新启动了我的电脑,它现在似乎可以工作了。 叹息

标签: php wordpress api plugins shortcode


【解决方案1】:

这里是如何使用带有属性的短代码

function bartag_func( $atts ) {
    extract( shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts ) );

    return "foo = {$foo}";
}
add_shortcode( 'myshortcode', 'bartag_func' );

[myshortcode foo="bar" bar="bing"]

你缺少提取的东西

【讨论】:

  • extract 方法不只是将值提取出来并放入它们自己的值中吗??
  • 我的问题是,似乎根本没有传递属性。
  • 好的,所以我确实使用了 shortcode_atts 和 extract。 shortcode_atts 会覆盖我的价值观,即使它被认为是通过了。据我了解,shortcode_atts 假设在未传递值的情况下提供默认值。问题是我正在传递一个值,但它作为 null 进入函数。
【解决方案2】:

也许您需要像文档中所说的那样使用提取功能:

// [bartag foo="foo-value"]
function bartag_func( $atts ) {
    extract( shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts ) );

    return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );

http://codex.wordpress.org/Shortcode_API

【讨论】:

  • $atts 以 null 的形式出现。所有这一切都会用默认值覆盖 null 的值。但我正在向 $atts 传递一些东西。我不明白为什么它是空的。
猜你喜欢
  • 2023-04-05
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多