get_extended()
云策文档标注
概述
get_extended() 函数用于从文章内容中提取“更多”标签前后的文本,返回一个包含主内容、扩展内容和自定义“阅读更多”文本的数组。它主要用于处理 WordPress 文章中的 标签。
关键要点
- 函数解析文章内容中的 标签,分割为“main”(标签前内容)和“extended”(标签后内容)部分。
- 返回数组包含 'main'、'extended' 和 'more_text' 键,其中 'more_text' 可存储自定义“阅读更多”文本。
- 使用正则表达式匹配标签,并自动去除内容的前后空白字符。
- 在 XML-RPC 相关函数(如 wp_xmlrpc_server::mw_getPost())中常用,以支持远程文章检索。
代码示例
$post = get_post();
$myposts = get_posts( array(
'posts_per_page' => 5
) );
foreach( $myposts as $post ) : setup_postdata( $post );
$content_arr = get_extended ( $post->post_content );
?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div><?php echo $content_arr['main']; ?></div>
<?php
endforeach;注意事项
- 在 标签中,第二个破折号后和单词“more”前不应有空格,否则可能影响匹配。
- 标签后可以跟随文本或空格,但这些不会被函数引用或处理。
- 返回的 'extended' 键包含 标签后的内容,可用于进一步显示或处理。
原文内容
Gets extended entry info (<!--more-->).
Description
There should not be any space after the second dash and before the word ‘more’. There can be text or space(s) after the word ‘more’, but won’t be referenced.
The returned array has ‘main’, ‘extended’, and ‘more_text’ keys. Main has the text before the <!--more-->. The ‘extended’ key has the content after the <!--more--> comment. The ‘more_text’ key has the custom “Read More” text.
Parameters
$poststringrequired-
Post content.
Source
function get_extended( $post ) {
// Match the new style more links.
if ( preg_match( '/<!--more(.*?)?-->/', $post, $matches ) ) {
list($main, $extended) = explode( $matches[0], $post, 2 );
$more_text = $matches[1];
} else {
$main = $post;
$extended = '';
$more_text = '';
}
// Leading and trailing whitespace.
$main = preg_replace( '/^[s]*(.*)[s]*$/', '\1', $main );
$extended = preg_replace( '/^[s]*(.*)[s]*$/', '\1', $extended );
$more_text = preg_replace( '/^[s]*(.*)[s]*$/', '\1', $more_text );
return array(
'main' => $main,
'extended' => $extended,
'more_text' => $more_text,
);
}
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |
Skip to note 2 content
Codex
Displaying small excerpts from latest posts
If you want to display the latest posts on your WordPress blog, but only the content which comes before the
<!--more-->tag, you can use this:<ul> $post = get_post(); $myposts = get_posts( array( 'posts_per_page' => 5 ) ); foreach( $myposts as $post ) : setup_postdata( $post ); $content_arr = get_extended ( $post->post_content ); ?> <li> <a href="<?php the_permalink(); ?>"></a> </br> </li> </ul>Note:
$content_arr['extended']contains the contents after the more tag.