gpt4 book ai didi

WordPress 替换页面内容(插件)

转载 作者:行者123 更新时间:2023-12-05 09:25:32 29 4
gpt4 key购买 nike

我正在编写一个插件来添加带有标签 [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");

我刚刚注意到我为应该替换标签和函数本身的内容赋予了相同的变量名。这可能是问题所在吗?

最佳答案

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

关于WordPress 替换页面内容(插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1867490/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com