【问题标题】:WordPress replace content of a page (plugin)WordPress替换页面内容(插件)
【发布时间】:2009-12-08 14:54:16
【问题描述】:

我正在编写一个插件,它添加一个带有标签 [deposit_page] 的页面;该标签应替换为一些 PHP 代码。

这是我拥有的,但它不起作用。我有什么遗漏或做错了吗?

function deposit_page_content($content) {
    $deposit_page_content = "here will go the content that should replace the tags";//end of variable deposit_page_content
    $content=str_ireplace('[deposit_page]',$deposit_page_content,$content);
return $content;
}
add_filter("the_content", "deposit_page_content");

我刚刚注意到我为应该替换标记和函数本身的内容赋予了相同的变量名称。会不会是这个问题?

【问题讨论】:

  • 该代码运行良好(我只是通过将其放入functions.php 中对其进行了测试)。您如何将其包含在您的插件中?你的插件还有更多功能吗?如果没有,我建议只需将其粘贴到您的函数文件中。
  • 我做到了,我只是将函数复制/粘贴到默认主题的functions.php中,但还是一样
  • 你在使用 PHP5 吗? str_ireplace 是一个 PHP 5 函数。正如我所说,我在这里将它复制并粘贴到现有的 wp-install 中,它工作正常。就其本身而言,在句子中间(“test [deposit_page] test”),几乎是我能想到的任何配置。这是完全有效的代码。
  • 它在 wamp 上,所以是的,它是 php 5。我将在另一个 wordpress 安装上尝试一下

标签: wordpress plugins


【解决方案1】:

WordPress 支持内置 [square_bracket_shortcodes]

见:http://codex.wordpress.org/Shortcode_API

这是您的简单示例:

function deposit_page_shortcode( $atts ) {
    $deposit_page_content = "here will go the content that should replace the tags";
    return $deposit_page_content;
}

add_shortcode( 'deposit_page', 'deposit_page_shortcode' );

您可以将其粘贴到您的活动主题的 functions.php 文件中。

如果你想要属性,比如[my_shortcode foo=bar],你可以在这个函数的顶部做:

extract(shortcode_atts(array(
    'foo' => 'default foo',
    'example' => 'default example',
), $atts));

// Now you can access $foo and $example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 2014-05-19
    • 2016-12-20
    • 2019-03-23
    • 1970-01-01
    • 2023-04-05
    • 2011-05-22
    相关资源
    最近更新 更多