模板標籤 the_content() 顯示當前文章的內容。該標籤必須在 WordPress 主迴圈 (loop) 中。若文章使用快速標籤 <!--more--> 來擷取摘要,the_content() 標籤將只在非單篇文章或非固定連結文章上顯示 <!--more--> 前的摘要部分。 the_content() 標籤可包含一個規定 <!--more--> 內容和樣式的引數,該引數會生成 「繼續閱讀全文」 的連結。

關於 <!--more--> 標籤有以下三條規定:

<!--more--> 快速標籤中的 more 前不得有空格。否則 <!--more--> 將無法發揮作用。
<!--more--> 快速標籤無法在模板中執行 (會被模板忽略),如 single.php 只會顯示一篇文章。
改變 「READ MORE」 的樣式

我們在部落格首頁顯示日誌摘要時,總希望讀者能夠點選日誌標題或某個連結,繼續閱讀日誌全文。 WordPress 就為我們提供了自定義 「Read More(閱讀全文)」 連結樣式的機會。

WordPress 透過兩種方法顯示日誌摘要。第一種,用模板標籤 the_excerpt() 代替 the_content(),之後我們在管理頁面中 「新增新文章」 下的 「摘要」 中輸入的內容就會出現在部落格首頁上。如果沒有在 「摘要」 中輸入內容,日誌的前 55 個單詞會作為摘要被顯示在首頁上。當訪問者閱讀摘要後,希望瞭解更多相關資訊時,可以點選日誌標題閱讀全文。

而在 WordPress 中,顯示日誌的最常用方法是:保留 the_content() 模板標籤,在日誌中選擇一個適當的位置 (預設一個摘要擷取位置),插入一個名為 more 的快速標籤 (quicktag) 。

控制板 「新增新文章」 中編輯視窗上方的小圖示被稱為快速標籤。這些圖示包括加粗、斜體、新增連結等,以及大名鼎鼎的 「插入 more 標籤」 。把遊標放在日誌中希望結束摘要的位置上,點選 「(插入 more 標籤)」 。在日誌被截斷的地方會插入類似下面的程式碼:
and I told him that he should get moving or I'd be

on him like a limpet.  He looked at me with shock on

his face and said

<!--more-->
程式碼後是日誌的剩餘部分,但如果在存檔、類別、首頁以及搜尋結果等非單頁型/非永久連結型頁面上,日誌會只顯示 「more」 標籤前的內容,作為摘要。

模板標籤 the_content() 的引數如下:

1 <?php
2     the_content( $more_link_text , $strip_teaser, $more_file );
3 ?>
$more_link_text 將連結文字設定為類似 「Read More」 的內容。第二個引數 $strip_teaser 決定是 (TRUE) 否 (FALSE) 隱藏 「more」 連結,該引數的預設值為 FALSE——不隱藏 more 連結。最後一個引數 $more_file 將連結連線到我們所希望的 「Read More」 被連結到的內容中。預設情況下,$more_file 連結到當前日誌檔案。

如果不希望顯示摘要,那麼可以將 index.php 中的 the_content(); 更改為 (例如讓第二個引數控制屬性值):

1 the_content('',FALSE,'');
或者在在 <!--more--> 後的日誌正文中立即包含<!--noteaser--> 。

預設情況下,訪問者點選 「Read More」 連結時,網頁會載入日誌內容並將訪問者帶領到日誌中<--more--> 標籤的位置上。如果我們不希望將訪問者導向到這個位置,可以在主題的 functions.php 檔案中加入以下程式碼:

01 function remove_more_jump_link($link)
02 {
03     $offset = strpos($link, '#more-');
04     if ($offset)
05     {
06         $end = strpos($link, '"',$offset);
07     }
08     if ($end)
09     {
10         $link = substr_replace($link, '', $offset, $end-$offset);
11     }
12     return $link;
13 }
14 add_filter('the_content_more_link', 'remove_more_jump_link');
在 WP 2.7.1 以及之前的版本中,可以在 wp-includes/post-template.php 中編輯以下程式碼行,以此改變控制 more 指向內容的預設函式 (注意:在 WP 2.1 之前,以下程式碼出現在 wp-includes/template-functions-post.php 中) 。

注意:升級 WordPress 時,該檔案會被複原,因此我們需要儲存所做修改,升級完畢後再重新修改檔案。

我們需要將:

1 $output .= ' <a href="'.%20get_permalink()%20."#more-$id">$more_link_text</a>";
改為

1 $output .= ' <a href="'.%20get_permalink()%20."">$more_link_text</a>";

1 $output .= ' <a href="'.%20get_permalink()%20.'">$more_link_text</a>';
具體實現函式如下:

01 /**
02  * Retrieve the post content.
03  *
04  * @since 0.71
05  *
06  * @param string $more_link_text Optional. Content for when there is more text.
07  * @param string $stripteaser Optional. Teaser content before the more text.
08  * @return string
09  */
10 function get_the_content($more_link_text = null, $stripteaser = 0) {
11     global $id, $post, $more, $page, $pages, $multipage, $preview,$pagenow;
12     if ( null === $more_link_text )
13         $more_link_text = __( '(more...)' );
14     $output = '';
15     $hasTeaser = false;
16     // If post password required and it doesn't match the cookie.
17     if ( post_password_required($post) ) {
18         $output = get_the_password_form();
19         return $output;
20     }
21     if ( $page > count($pages) ) // if the requested page doesn't exist
22         $page = count($pages); // give them the highest numbered page that DOES exist
23     $content = $pages[$page-1];
24     if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
25         $content = explode($matches[0], $content, 2);
26         if ( !empty($matches[1]) && !empty($more_link_text) )
27             $more_link_text =strip_tags(wp_kses_no_null(trim($matches[1])));
28         $hasTeaser = true;
29     } else {
30         $content = array($content);
31     }
32     if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
33         $stripteaser = 1;
34     $teaser = $content[0];
35     if ( ($more) && ($stripteaser) && ($hasTeaser) )
36         $teaser = '';
37     $output .= $teaser;
38     if ( count($content) > 1 ) {
39         if ( $more ) {
40             $output .= '<span id="more-' . $id . '"></span>' .$content[1];
41         } else {
42             if ( ! empty($more_link_text) )
43                 $output .= apply_filters( 'the_content_more_link', ' <a href="' .%20get_permalink()%20.%20"#more-$id" class="more-link" target="_blank">$more_link_text</a>", $more_link_text );
44             $output = force_balance_tags($output);
45         }
46     }
47     if ( $preview ) // preview fix for javascript/ target=_blank class=infotextkey>javascript bug with foreign languages
48         $output =   preg_replace_callback('/%u([0-9A-F]{4})/', create_function('$match', 'return "&#" . base_convert($match[1], 16, 10) . ";";'), $output);
49     return $output;
50 }
如果想點選 more 時彈出新頁面,可以修改以上函式,加入 target="_blank" 即可。

瞭解 「Read More」 的執行原理後,我們可以嘗試把 「Read More」 內容變得更有趣些,激發訪問者的閱讀興趣。

經過設計, the_content() 標籤包含了一個可以設計<!--more--> 內容和樣式的引數,而<!--more--> 標籤生成 「continue reading(繼續閱讀全文)」 的連結。

預設情況下,一個帶有 「more」 的摘要可以是下面這樣:

1 and I told him that he should get moving or I'd be on him
2 like a limpet. He looked at me with shock on his face and
3 said more...
如果希望將 more 換成其它單詞,只要在標籤中輸入新單詞就可以了:

1 <?php the_content('Read on...'); ?>
也可以將 more 換成幽默的句子:

1 <?php the_content('...on the edge of your seat? Click
2 here to solve the mystery.'); ?>
還可以在標籤中設計文字樣式:

1 <?php the_content('<span class="moretext">...on the edge of
2 your seat? Click here to solve the mystery.</span>'); ?>
之後在 style.css 樣式表單中,將 moretext 設為自己想要顯示的內容。下面的摘要示例使用了加粗字型與斜體,比預設文字字號稍小,示例用 font-variant: small-caps 強制摘要內容顯示為小寫字母:

1 and I told him that he should get moving or I'd be on him
2 like a limpet. He looked at me with shock on his face and
3 said ...On the Edge of Your Seat? Click Here to Solve the
4 Mystery.
有些使用者不希望在摘要中顯示 「Read More」 等文字,他們希望用擴充套件字元或 HTML 字元實體將讀者導向到日誌全文。

1 <?php the_content('» » » »'); ?>
模板標籤 the_content() 中還有一個引數可以在 more 的位置上顯示日誌標題。用 the_title() 標籤包含日誌標題:

1 <?php the_content("...continue reading the story
2 called " . get_the_title('', '', false)); ?>
根據上文的描述,我們通常從模板中呼叫帶有標準文字的 the_content() 。但我們可以為一些日誌設定其它的 「more」 顯示方式。在視覺化編輯器中,輸入<!--more Your custom text --> 。

CSS 為我們提供了無限的設計可能,WordPress 也允許我們在很多模板標籤中使用圖片,包括 more 標籤。有兩種方法可以在 more 標籤中插入圖片。簡單的方法是——在 模板標籤 the_content() 中展示圖片。

1 <?php the_content('Read more...<img src="/images/leaf.gif"
2 alt="read more" title="Read more..." />'); ?>
注意:上述程式碼在圖片標籤中使用了 ALT 和 TITLE 屬性。這是為了符合網路標準以及保證圖片的可訪問性,因為圖片不僅是圖片,同時也是一個連結。

我們甚至可以根據上一個章節中的描述進一步改造圖片和 more 標籤。如果只想使用圖片二不想顯示 「Read More」,可以刪除 「Read More」 字樣。

第二個示例使用的是 CSS 背景圖片。我們在之前的例子中以及論述過怎樣使用樣式類。這個例子稍微複雜一些。容器的樣式必須設為允許背景圖片顯示在文字後。如果我們用上一個示例做背景圖,其 style.css 樣式表單應該顯示為:

1 .moretext {
2    width: 100px;
3    height: 45px;
4    background:url(/images/leaf.gif) no-repeat right middle;
5    padding: 10px 50px 15px 5px}
6 }
頁面右邊 50 畫素的內邊距能夠保證文字與圖片間的距離,保證兩者不相覆蓋。而高度則保證容器有足夠的寬度容納圖片,因為背景圖並不是 「實際存在」 的,也不可能與容器的邊框相抵。我們需要將圖片的大小和形狀都考慮進去,用最適當的方式顯示圖片。

掌握這其中的基本原理後,我們就可以隨心所欲地開始設計了。

切記,網站首頁 ( is_home() == TRUE ) 是不顯示 <!--more--> 標籤的,除非我們用以下程式碼來啟用顯示:

1 <?php
2 global $more;
3 $more = 0;
4 ?>
總結,模板標籤-the_content() 用法如下:

1 <?php the_content( $more_link_text, $strip_teaser, $more_file ); ?>
$more_link_text:(字串)(可選)「more」 連結的連結文字, 預設值: '(more...)'
$strip_teaser:(布林型)(可選) 顯示 (FALSE) 或隱藏 (TRUE)more 連結前的文字。預設值:FALSE
$more_file:(字串)(可選)more 連結所指向的檔案 預設值:當前檔案
使用以下方式在 「More」 中加入標題,在 the_title() 標籤和 display 引數的幫助下,若文章使用 ,該示例可以顯示"Continue reading ACTUAL POST TITLE"(繼續閱讀當前文章標題) 。

1 <?php the_content("Continue reading " . the_title('', '', false)); ?>
如果 the_content() 不能按計劃執行 (如當你希望顯示<!--more--> 標籤前的內容,the_content() 卻顯示了全文內容),你可以用全域性變數 $more 改寫這一行為:

1 <?php
2 global $more; // Declare global $more (before the loop).
3 $more = 0; // Set (inside the loop) to display content above the more tag.
4 the_content("More...");
5 ?>
如果需要顯示所有正文:

1 <?php
2 global $more; // Declare global $more (before the loop).
3 $more = 1; // Set (inside the loop) to display all content, including text below more.
4 the_content();
5 ?>
你可以用 get_the_content 函式返回文章內容的值,而非直接輸出文章內容。示例如下:

1 <?php $content = get_the_content(); ?>
請注意!get_the_content 無法進行以下操作,因此你最好手動新增以下程式碼,直到核心程式升級成功:

1 <?php
2 $content = apply_filters('the_content', $content);
3 $content = str_replace(']]>', ']]>', $content);
4 ?>
the_content() 位於 wp-includes/post-template.php