WordPress函数get_the_excerpt()获取文章摘要

WordPress函数get_the_excerpt()获取文章摘要

WordPress函数get_the_excerpt()获取文章摘要

  在 WordPress CMS内容管理系统中,get_the_excerpt()是一个内置函数,用于获取帖子的摘录。摘录是帖子的简短摘要,通常在单个帖子视图或摘要中使用。

  推荐:WordPress函数wp_signon()身份验证登录

get_the_excerpt()函数基本语法

描述

  检索帖子摘录

用法

echo get_the_excerpt();

参数

  $post

  (int|WP_Post可选)帖子 ID 或 WP_Post 对象。默认是全局的$post。默认值:null

  推荐:WordPress函数wp_list_comments()显示帖子评论列表

get_the_excerpt()函数

  get_the_excerpt()函数将首先检查帖子是否定义了明确的摘录。如果是,则该函数将返回该摘录。如果帖子没有明确的摘录,则该函数将自动生成摘录。自动摘录是通过获取帖子内容的前 55 个单词并在单词边界处修剪掉它们来生成的。(源文件可参考这里

function get_the_excerpt( $post = null ) {
	if ( is_bool( $post ) ) {
		_deprecated_argument( __FUNCTION__, '2.3.0' );
	}

	$post = get_post( $post );
	if ( empty( $post ) ) {
		return '';
	}

	if ( post_password_required( $post ) ) {
		return __( 'There is no excerpt because this is a protected post.' );
	}

	/**
	 * Filters the retrieved post excerpt.
	 *
	 * @since 1.2.0
	 * @since 4.5.0 Introduced the `$post` parameter.
	 *
	 * @param string  $post_excerpt The post excerpt.
	 * @param WP_Post $post         Post object.
	 */
	return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
}

  推荐:NEX-Forms插件下载WordPress表单生成器插件+ Addons

如何使用get_the_excerpt()

  显示帖子摘录

$excerpt = get_the_excerpt();
echo '<p>' . $excerpt . '</p>';

  使用 HTML 元描述摘录,在主题的头部添加此代码片段,以使用帖子摘录作为单个帖子页面的元描述:

<meta name="description" content="<?php echo wp_strip_all_tags(get_the_excerpt(), true); ?>" />

  限制手动摘录显示,要将显示的手册摘录限制为 260 个字符并在最后一个单词后截断,请将此代码片段放置在您的主题中the_excerpt():

$excerpt = get_the_excerpt();
$excerpt = substr($excerpt, 0, 260);
$result = substr($excerpt, 0, strrpos($excerpt, ' '));
echo $result;

  自定义帖子摘录长度,更改帖子摘录的长度

function custom_excerpt_length() {
    return 30; // Set the length to 30 words
}
add_filter('excerpt_length', 'custom_excerpt_length');

$excerpt = get_the_excerpt();
echo '<p>' . $excerpt . '</p>';

  在帖子摘录中添加“阅读更多”链接

$excerpt = get_the_excerpt();
$read_more_link = '<a href="' . get_permalink() . '">Read More</a>';
echo '<p>' . $excerpt . ' ' . $read_more_link . '</p>';

  如果未设置,则显示自定义摘录文本

$excerpt = get_the_excerpt();
if (empty($excerpt)) {
    $excerpt = 'No excerpt available for this post.';
}
echo '<p>' . $excerpt . '</p>';

  在条件语句中使用帖子摘录,在条件语句中使用帖子摘录来执行特定操作。

$excerpt = get_the_excerpt();
if (strlen($excerpt) > 100) {
    echo '<p class="long-excerpt">' . $excerpt . '</p>';
} else {
    echo '<p class="short-excerpt">' . $excerpt . '</p>';
}

  推荐:如何检查WordPress主题是否存在恶意代码

使用get_the_excerpt()过滤器

  基本用法

  • $post_excerpt (string) – The post excerpt.帖子摘录
  • $post (WP_Post) – Post object.发布对象
function your_custom_function( $excerpt, $post ) {
    // your custom code here
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'your_custom_function', 10, 2 );

  防止 WP REST API 中的自动摘录,通过以下过滤器挂钩禁用从 the_content 自动生成摘录。

remove_filter('get_the_excerpt', 'wp_trim_excerpt');

  限制摘录长度,将帖子摘录限制为 30 个字符。

function limit_excerpt_length( $excerpt ) {
    if ( strlen( $excerpt ) > 30 ) {
        $excerpt = substr( $excerpt, 0, 30 ) . '...';
    }
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'limit_excerpt_length' );

  添加阅读更多链接,在帖子摘录末尾添加“阅读更多”链接。

function add_read_more_link( $excerpt, $post ) {
    $read_more_link = ' <a href="' . get_permalink( $post ) . '">Read More</a>';
    return $excerpt . $read_more_link;
}
add_filter( 'get_the_excerpt', 'add_read_more_link', 10, 2 );

  显示空摘录的自定义消息,当帖子摘录为空时,显示“无摘录可用”。

function custom_empty_excerpt_message( $excerpt ) {
    if ( empty( $excerpt ) ) {
        $excerpt = 'No excerpt available.';
    }
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'custom_empty_excerpt_message' );

  从摘录中删除短代码,从帖子摘录中删除所有短代码。

add_filter('get_the_excerpt', 'remove_shortcodes_from_excerpt', 10, 2);

function remove_shortcodes_from_excerpt($post_excerpt, $post) {
  return strip_shortcodes($post_excerpt);
}

  摘录大写,将帖子摘录的第一个字母大写。

  向摘录添加自定义前缀,此示例向摘录添加自定义前缀“Summary:”。

add_filter('get_the_excerpt', 'add_custom_prefix_to_excerpt', 10, 2);

function add_custom_prefix_to_excerpt($post_excerpt, $post) {
  return 'Summary: ' . $post_excerpt;
}

  根据帖子类别修改摘录,此示例根据帖子类别向摘录添加自定义前缀。

add_filter('get_the_excerpt', 'modify_excerpt_based_on_category', 10, 2);

function modify_excerpt_based_on_category($post_excerpt, $post) {
  if (in_category('news', $post)) {
    return 'News: ' . $post_excerpt;
  } elseif (in_category('events', $post)) {
    return 'Event: ' . $post_excerpt;
  }
  return $post_excerpt;
}

  推荐:WordPress函数get_boundary_post()获取边界文章

  推荐:WordPress函数使用手册

5/5 - (1 vote)

晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数get_the_excerpt()获取文章摘要

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

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部