WordPress函数get_extended()获取扩展信息

WordPress函数get_extended()获取扩展信息

WordPress函数get_extended()获取扩展信息

  在 WordPress CMS内容管理系统中,get_extended()是一个内置函数,WordPress PHP 函数检索扩展条目信息,特别是帖子中标签前后的内容在本文中,我们将向您展示如何使用get_extended()函数。

  推荐:什么是Trackback?WordPress怎么禁用Trackback

get_extended()函数基本语法

  第二个破折号之后和“更多”一词之前不应有任何空格。“更多”一词后可以有文字或空格,但不会被引用。

  返回的数组包含“main”、“extended”和“more_text”键。Main 之前有文本<!--more-->。’extended’ 键包含注释后的内容<!--more-->。’more_text’ 键具有自定义的“阅读更多”文本。

// Get the post
$post = get_post();

// Use get_extended() to retrieve content before and after <!--more-->
$content_arr = get_extended($post->post_content);

// Display the content before and after <!--more-->
echo $content_arr['main']; // Content before <!--more-->
echo $content_arr['extended']; // Content after <!--more-->

描述

  检索与当前帖子相邻的上一篇帖子

用法

$content_arr = get_extended($post_content);

参数

  $post

  (string ) ( 必须) 要解析的帖子内容

返回值

  String[]. 在(’main’)之前发布,在(’extended’)之后发布,并自定义阅读更多内容(’more_text’)

Array(
	[main]      => 'some text', // text before the `<!--more-->` comment.
	[extended]  => 'some text', // text after the `<!--more-->` comment.
	[more_text] => ''           // if more is specified as: <!‐‐more reed more…‐‐>,
								// then this value will contain "reed more…"
)

  推荐:如何修复WordPress后台响应速度慢

get_extended()函数

  WordPress函数get_extended()获取扩展信息(源文件可参考这里

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,
	);
}

  推荐:WordPress函数get_adjacent_post()获取相邻文章

如何使用get_extended()

  显示自定义阅读更多文本,更改标记后显示的文本more:

// Get the post
$post = get_post();

// Use get_extended() to retrieve content before and after <!--more-->
$content_arr = get_extended($post->post_content);

// Display the content before and after <!--more-->
echo $content_arr['main']; // Content before <!--more-->
echo $content_arr['more_text']; // Custom "Read More" text

  显示最新帖子的摘录,显示您的 WordPress 博客上的最新帖子,但仅显示标签前的内容more:

$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(); ?>"><?php the_title(); ?></a>
        <br>
        <?php echo $content_arr['main']; ?>
    </li>
<?php endforeach; ?>

  使用自定义阅读更多链接显示完整内容,显示帖子的完整内容,带有自定义阅读更多链接:

/ Get the post
$post = get_post();

// Use get_extended() to retrieve content before and after <!--more-->
$content_arr = get_extended($post->post_content);

// Display the content and a custom Read More link
echo $content_arr['main']; // Content before <!--more-->
echo $content_arr['extended']; // Content after <!--more-->
echo '<a href="' . get_permalink($post->ID) . '">' . $content_arr['more_text'] . '</a>'; // Custom "Read More" link

  在主要内容和扩展内容之间添加自定义分隔符,使用自定义分隔符显示主要内容和扩展内容:

// Get the post
$post = get_post();

// Use get_extended() to retrieve content before and after <!--more-->
$content_arr = get_extended($post->post_content);

// Display the content with a custom separator
echo $content_arr['main']; // Content before <!--more-->
echo ' [Custom Separator] ';
echo $content_arr['extended']; // Content after <!--more-->

  推荐:WordPress函数get_next_post()获取下一篇文章

  推荐:WordPress函数使用手册

给文章评分

晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数get_extended()获取扩展信息

转载请保留链接:https://www.pythonthree.com/wordpress-get-extended/

Claude、Netflix、Midjourney、Chatgpt Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
Kinsta-free-hosting主机
Kinsta-free-hosting主机
Elementor可视化创建WordPress网站
Elementor可视化创建WordPress网站
滚动至顶部