【问题标题】:short code not working in wordpress text editor短代码在 wordpress 文本编辑器中不起作用
【发布时间】:2014-03-19 23:14:55
【问题描述】:

我想从 wordpress 帖子的文本编辑器中添加短代码。我在 wordpress 的帖子中添加了以下短代码:

<img alt="" src="[template_url]/images/ic_sol_1a.png" />

这里[template_url] 是我想使用的简码,它不起作用。当我在帖子页面中看到它时,它会呈现文本而不是短代码响应。在某处我看到了一个解决方案,例如在主题的functions.php中添加以下行:

add_filter( 'widget_text', 'shortcode_unautop');

add_filter( 'widget_text', 'do_shortcode');

但在添加这些行之后,我仍然无法使短代码工作。可能的原因是什么?

我的简码函数是这样的:

function my_template_url() {
  return get_bloginfo('template_url'); 
}
add_shortcode("template_url", "my_template_url");

【问题讨论】:

  • 你把你的简码函数放在哪里了?这是在你的主题的functions.php文件中吗?您提到这是在 WordPress 帖子的文本编辑器中,因此您显示的过滤器不适用。你在哪个文件中输出这个?您能否为此特定部分添加模板标记?
  • @NathanDawson 我已在主题的functions.php 中添加了该短代码函数,并在创建帖子时在帖子文本编辑器中使用了短代码[template_url]。
  • 请包含single.php的内容
  • @NathanDawson 感谢您的提示,我们遇到了问题,我将在答案中添加。

标签: php wordpress shortcode


【解决方案1】:

您需要使用get_bloginfo() 来返回您的示例中的值,并确保使用the_content() 将值打印到页面。如果您使用其中一种 API 方法,例如 get_the_content(),则不会运行 do_shortcode 过滤器。如果您想应用它,请使用以下内容:

function my_template_url() {
    // This will echo the 'template_url'
    return get_bloginfo('template_url'); 
}
add_shortcode("template_url", "my_template_url");

$shortcoded = apply_filters( 'the_content', get_the_content() );

您不需要add_filter() 行。

【讨论】:

  • 您的逻辑是正确的。我尝试按照您说的方式实现它,但无法使其正常工作?
  • 感谢您帮助我们解决了问题。你的逻辑也有很大帮助。我将在下面添加正确答案
【解决方案2】:

在经历了很多头痛之后,在@doublesharp 和@Nathan Dawson cmets/answers 的帮助下,我们发现问题出在主题的single.php 文件中,我们使用get_the_content 函数获取了帖子的内容。通过将其更改为 the_content() 函数 sortcodes 开始工作。 请注意短代码在管理网站上的 WordPress 帖子的可视化编辑器中不起作用,但是当您在浏览器中实际看到该帖子时,您可以看到它在工作。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多