WordPress的简码不呈现

问题描述

我正在尝试使用自定义插件添加简码WordPress网站。无论我如何尝试注册调用它,短代码似乎都不会呈现。

我正在使用wordpress认2020主题对此进行测试。

这是添加简码的代码

    add_shortcode(
        'test',function () {
            return 'test output';
        }
    );

如果我手动检查模板上的内容

    var_dump(shortcode_exists('test'));

以下是在页面加载时生成的:

boolean true

但是,如果将以下内容添加页面内容中,则不会加载:

[test]

我还为主题functions.PHP

添加了以下内容
add_filter( 'widget_text','do_shortcode' );

但是,因为这不在小部件内(仅张贴文字),所以我这样做是为了消除这种可能性。它没有帮助。

编辑: 我还在页面添加了以下内容

    do_shortcode(['test']);

这将产生所需的输出

每个教程都做到上述,几乎逐字记录。 我想念什么?

要清楚:在内容中使用[test]在帖子正文内容显示简码是必需的行为。

解决方法

尝试更改测试名称,突然将其保留,其次,打印整个短代码

print do_shortcode('[mytest]'); // [test]

<?php
/*
Plugin Name: Test
Description: La la la fa
Version: 0.1.0
*/

function question($attrs,$content = null) {
  return '???';
}
add_shortcode('question','question');

add_action('init',function() {
  d(do_shortcode('Are you sure [question]'));
});

enter image description here

使用短代码处理输出内容的方法

the_content();
get_the_content();
apply_filters('the_content',$post->post_content); 

示例:

add_action('init',function() {
  d(apply_filters('the_content','Are you sure [question]')); // $post->post_content
});

enter image description here

,

这是一个自定义测试模板,帖子的内容不会自动分析。 实际上,我为每个帖子创建了大量的自定义元数据,这些帖子是我在内容之外安排和展示的,因此我使用了$post->post_content属性。

请注意... $post->post_content未解析,the_content()

现在一切正常。 这是一个很好的陷阱。