本文介绍了WordPress:get_the_content() 和 the_content() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 WordPress 中开发新主题,并在 get_the_content() 函数上花费了大量时间.

 

<div><p><?=get_the_content();?></p>

好像不处理快捷方式,也不做段落.

然后我将其替换为 the_content();我的段落和快捷方式开始起作用了.

 

<div><p><?=the_content();?></p>

我的问题:函数之间有什么区别和什么额外的处理 the_content();是否与 get_the_content(); 比较;?

解决方案

虽然@J Quest 提供了足够的答案,但我想详细说明一下.一般来说,WordPress 有两种类型的后变量函数:get_ 函数和 the_ 函数.

get_ 函数,例如 get_the_content()get_the_ID() 将返回所需的信息,然后必须对其进行操作并打印到页面上.一些例子:

$content = get_the_content();$content = apply_filters('the_content', $content);$content = str_replace( 'foo', 'bar', $content );回声'发布#'.get_the_ID() .$内容;

the_ 函数,例如 the_content()the_ID() 实际上 echo 返回的值,如果适用,将为适当的值应用默认过滤器".这些函数不需要回显.

echo get_the_ID();

在功能上与

相同

the_ID();

如果您查看 the_ID() 的文档,您会看到它实际上只是输出 get_the_ID() 的值.来源:

function the_ID() {echo get_the_ID();}

在这种情况下,如果您尝试将 the_ 函数设置为变量,则会在整个页面中留下一串回显变量.

$id = the_ID();echo '帖子ID:'.$id;

将输出:

123帖子ID:123

要使用 get_the_content() 并运行短代码,您需要通过 do_shortcode() 函数,或者更好的 the_content 过滤器.

$content = get_the_content();echo do_shortcode( $content );//或者:echo apply_filters( 'the_content', $content );

如果您只需要在模板中吐出帖子内容,而不进行任何操作,通常最好使用(无回声或回声短标签):

the_content();

I was working on the new theme in the WordPress and spent tons of time with the get_the_content() function.

  <div class="clearfix">
      <div>
        <p><?=get_the_content();?></p>
      </div>
  </div>

Seems that it doesn't process shortcuts and doesn't do paragraphs.

Then I replaced it with the the_content(); and my paragraphs and shortcuts started to work.

  <div class="clearfix">
      <div>
        <p><?=the_content();?></p>
      </div>
  </div>

My question: What is the difference between the functions and what additional processing the_content(); does comparing to get_the_content();?

解决方案

While @J Quest provided an adequate answer, I'd like to elaborate a little bit. Generally speaking, WordPress has two types of post variable functions: get_ functions and the_ functions.

get_ functions, such as get_the_content() or get_the_ID()will return the desired information, which must then be manipulated and printed to the page. Some examples:

$content = get_the_content();
$content = apply_filters( 'the_content', $content );
$content = str_replace( 'foo', 'bar', $content );

echo 'Post #'. get_the_ID() . $content;

the_ functions, such as the_content() and the_ID() actually echo the returned value, and if applicable will apply the "default filters" for the appropriate values. These functions don't need to be echoed.

echo get_the_ID();

is functionally the same as

the_ID();

If you look at the docs for the_ID() you'll see it literally just outputs the value of get_the_ID(). From the source:

function the_ID() {
    echo get_the_ID();
}

In that vein, if you try and set the_ functions as a variable, you'll leave a trail of echoed variables throughout the page.

$id = the_ID();
echo 'Post ID: '.$id;

will output:

123Post ID: 123

To use get_the_content() and get shortcodes to run, you'll either need to run it through the do_shortcode() function, or better yet the_content filter.

$content = get_the_content();

echo do_shortcode( $content );
// Or:
echo apply_filters( 'the_content', $content );

If you just need to spit out post content in a template, without any manipulation, you're typically better off with (no echo or echo short tag):

the_content();

这篇关于WordPress:get_the_content() 和 the_content() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:25