首页 文章

短代码不能在wordpress文本编辑器中工作

提问于
浏览
1

我想在wordpress post的文本编辑器中添加短代码 . 我在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");

2 回答

  • 2

    您将需要使用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() 行 .

  • 1

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

相关问题