一、官方解釋
首先還是引用下官方文檔的説明,比較權威。
1 、 get_the_content 解釋:
Retrieve the post content. (Must be used in a Loop)
2 、 the_content 解釋:
Displays the contents of the current post. This tag must be within The_Loop.
個人理解是,get_the_content 返回的是 post 的內容,而 the_content 則直接輸出內容。到底是不是這樣的呢?看下源碼吧:
二、官方源碼
1 、 get_the_content 源碼
- /**
- * Retrieve the post content.
- *
- * @since 0.71
- *
- * @param string $more_link_text Optional. Content for when there is more text.
- * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
- * @return string
- */
- function get_the_content($more_link_text = null, $stripteaser = false) {
- global $post, $more, $page, $pages, $multipage, $preview;
- if ( null === $more_link_text )
- $more_link_text = __( '(more...)' );
- $output = '';
- $hasTeaser = false;
- // If post password required and it doesn't match the cookie.
- if ( post_password_required($post) )
- return get_the_password_form();
- if ( $page > count($pages) ) // if the requested page doesn't exist
- $page = count($pages); // give them the highest numbered page that DOES exist
- $content = $pages[$page-1];
- if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
- $content = explode($matches[0], $content, 2);
- if ( !empty($matches[1]) && !empty($more_link_text) )
- $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
- $hasTeaser = true;
- } else {
- $content = array($content);
- }
- if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) ¦¦ ($page==1))) )
- $stripteaser = true;
- $teaser = $content[0];
- if ( $more && $stripteaser && $hasTeaser )
- $teaser = '';
- $output .= $teaser;
- if ( count($content) > 1 ) {
- if ( $more ) {
- $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
- } else {
- if ( ! empty($more_link_text) )
- $output .= apply_filters( 'the_content_more_link', ' <a href="'%20.%20get_permalink()%20.%20"#more-{$post->ID}" class="more-link">$more_link_text</a>", $more_link_text );
- $output = force_balance_tags($output);
- }
- }
- if ( $preview ) // preview fix for javascript/ target=_blank class=infotextkey>javascript bug with foreign languages
- $output = preg_replace_callback('/%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output);
- return $output;
- }
2 、 the_content 源碼
- /**
- * Display the post content.
- *
- * @since 0.71
- *
- * @param string $more_link_text Optional. Content for when there is more text.
- * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
- */
- function the_content($more_link_text = null, $stripteaser = false) {
- $content = get_the_content($more_link_text, $stripteaser);
- $content = apply_filters('the_content', $content);
- $content = str_replace(']]>', ']]>', $content);
- echo $content;
- }
不需要太仔細研究源碼,但從函數的最後可以看出,分別是輸出和返回,和預想的一樣。
三、遇到的問題
既然這樣的話,那麼 get_the_content 和 the_content 的內容應該是一致的對吧。但是,在前台得到的結果卻是不一樣的:
1 、 get_the_content 的結果
- 這是段落 1
- 這是段落 2
2 、 the_content 的結果
- <p> 這是段落 1</p>
- <p> 這是段落 2</p>
出現的內容是,get_the_content 的值沒有 p 標籤包裹而 the_content 的內容有 p 標籤包裹,那麼如何解決呢?
3 、解決方法
可以自定義一個函數:
- function yundanran_get_the_content($more_link_text = null, $stripteaser = false)
- {
- $content = get_the_content($more_link_text, $stripteaser);
- $content = apply_filters('the_content', $content);
- $content = str_replace(']]>', ']]>', $content);
- return $content;
- }
函數和 the_content 類似,只是結果是 return 出來的。