目录
描述
Displays the contents of the current post. This template tag must be within The_Loop.
If the quicktag <!--more--> is used in a post to designate the "cut-off" point for the post to be excerpted, the_content() tag will only show the excerpt up to the <!--more--> quicktag point on non-single/non-permalink post pages. By design, the_content() tag includes a parameter for formatting the <!--more--> content and look, which creates a link to "continue reading" the full post.
Note about <!--more--> :
- No whitespaces are allowed before the "more" in the <!--more--> quicktag. In other words <!-- more --> will not work!
- The <!--more--> quicktag will not operate and is ignored in Templates where just one post is displayed, such as single.php.
- Read Customizing the Read More for more details.
用法
<?php the_content( $more_link_text, $stripteaser ); ?>
How to pass parameters to tags with PHP function-style parameters
参数
$more_link_text
(string) (可选) The link text to display for the "more" link.
默认值: '(more...)'
$stripteaser
(boolean) (可选) Strip teaser content before the more text.
默认值: false
示例
Designating the "More" Text
Displays the content of the post and uses "Read more..." for the more link text when the <!--more--> Quicktag is used.
1 2 3 4 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ <?php the_content('Read more...'); ?> |
Include Title in "More"
Similar to the above example, but thanks to the_title() tag and the display parameter, it can show "Continue reading ACTUAL POST TITLE" when the <!--more--> Quicktag is used.
1 2 3 4 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ <?php the_content("Continue reading " . get_the_title()); ?> |
Overriding Archive/Single Page Behavior
If the_content() isn't working as you desire (displaying the entire story when you only want the content above the <!--more--> Quicktag, for example) you can override the behavior with global $more.
1 2 3 4 5 6 7 8 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ <?php global $more; // Declare global $more (before the loop). $more = 0; // Set (inside the loop) to display content above the more tag. the_content("More..."); ?> |
if you need to display all of the content:
1 2 3 4 5 6 7 8 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ <?php global $more; // Declare global $more (before the loop). $more = 1; // Set (inside the loop) to display all content, including text below more. the_content(); ?> |
源文件
the_content() 函数的代码位于 wp-includes/post-template.php
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ /** * Display the post content. * * @since 0.71 * * @param string $more_link_text Optional. Content for when there is more text. * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false. */ function the_content( $more_link_text = null, $strip_teaser = false) { $content = get_the_content( $more_link_text, $strip_teaser ); /** * Filter the post content. * * @since 0.71 * * @param string $content Content of the current post. */ $content = apply_filters( 'the_content', $content ); $content = str_replace( ']]>', ']]>', $content ); echo $content; } |
the_content() 源文件
- 原文:http://codex.wordpress.org/Function_Reference/the_content
- 翻译:黄聪@WordPress之魂