the_content() 和 get_the_content() 的区别是什么?

the_content() 和 get_the_content() 的区别是什么?如果你是一名WordPress开发者,你可能知道WordPress函数the_content()可以直接输出文章内容,而get_the_content()则需要在前面添加echo才可以输出文章内容。

<?php
the_content();

echo get_the_content();
?>

这是WordPress内置函数的一个很重要的标志,the_开头的函数直接输出,get_开头的函数不执行输出。

然而,两者的区别不仅如此:

get_the_content()不会通过the_content传递内容 。这意味着它不会自动嵌入视频或扩展简码。所以,使用get_the_content(),它将删除嵌入和简码等标签。

get_the_content()获取的内容是原始保存的数据,不含段落标签p,导致原本应该分段的内容,无法进行分段。也即是说<?php echo get_the_content(); ?>和<?php the_content(); ?>输出的内容是不一样的,前者会过滤掉很多标签。所以,如果你要输出正文的完整内容时,请使用 <?php the_content(); ?>

最近折腾的项目就遇到这个问题,这次才搞明白<?php echo get_the_content(); ?>和<?php the_content(); ?>是真的不一样的。