WordPress自定义模板不支持短代码



我使用以下说明创建了一个空白模板。。。https://tips.initpals.com/wordpress-tips/how-to-create-a-blank-page-template-in-wordpress/

<?php /* Template Name: Blank Template */?>
<?php
if (have_posts()) {
while (have_posts()): the_post();
echo strip_tags(get_the_content(), '<p> <a>');
endwhile;
}
?>

但现在任何使用该模板的页面都不支持短代码。

启用短代码支持的代码是什么?我不是在寻找do_shortcode()函数,因为它只运行特定的短代码。。。我希望在页面中指定快捷代码,而不是在模板中指定。

你在找do_shortcode()吗?

搜索短代码的内容并通过其筛选短代码挂钩。如果没有定义快捷代码标签,则内容将在没有任何过滤的情况下返回。当插件已禁用,但短代码仍将显示在帖子中或内容。

源@https://developer.wordpress.org/reference/functions/do_shortcode/

<?php if ( have_posts() ):
while ( have_posts() ): the_post();
if ( get_the_content() !== '' ):
echo do_shortcode( get_the_content() );
endif; 
endwhile;
endif; ?>

未经测试但应该有效。

您的问题是get_the_content()不会通过负责扩展短代码和内容的the_content过滤器来传递帖子的内容。

此外,您声明您不想要do_shortcode(),因为它";"仅运行特定的短代码";。这是不正确的,它实际上会解析传递给它的字符串中的任何和所有短代码

如果使用the_content()函数,它会使用the_content过滤器,解析短代码,并自动返回结果。如果您使用的是get_the_content(),那么在手动输出结果之前,您需要通过do_shortcode()或使用apply_filters()对其应用the_content过滤器。

echo apply_filters( 'the_content', get_the_content() );

以及

echo do_shortcode( get_the_content() );

将解析传递的内容中的所有短代码。

相关内容

最新更新