WordPress函数the_excerpt过滤显示的帖子摘录

WordPress函数the_excerpt过滤显示的帖子摘录

WordPress函数the_excerpt过滤显示的帖子摘录

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

  the_excerpt()函数将首先检查帖子是否定义了明确的摘录。如果是,则该函数将显示该摘录。如果帖子没有明确的摘录,则该函数将自动生成摘录。自动摘录是通过获取帖子内容的前 55 个单词并在单词边界处修剪掉它们来生成的。

  推荐:The Plus Addon For Elementor插件Elementor扩展插件

the_excerpt函数基本语法

add_filter( 'the_excerpt', 'your_custom_function' );
function your_custom_function( $post_excerpt ) {
    // your custom code here
    return $post_excerpt;
}

描述

  过滤显示的帖子摘录

参数

  $post_excerpt

  int|WP_Post)可选。需要修改的帖子摘录

  推荐:8个最佳Chrome ChatGPT扩展程序

the_excerpt函数

  the_excerpt()函数是获取帖子简短摘要的有用方法。这对于在单个帖子视图或提要中显示非常有用。(源文件可参考这里

echo apply_filters( 'the_excerpt', get_the_excerpt() );

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

如何使用the_excerpt

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

add_filter( 'the_excerpt', 'custom_excerpt_length' );
function custom_excerpt_length( $post_excerpt ) {
    $limit = 20;
    $words = explode( ' ', $post_excerpt, $limit + 1 );
    if ( count( $words ) > $limit ) {
        array_pop( $words );
        $post_excerpt = implode( ' ', $words ) . '...';
    }
    return $post_excerpt;
}

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

add_filter( 'the_excerpt', 'add_read_more_link' );
function add_read_more_link( $post_excerpt ) {
    global $post;
    $read_more = ' <a href="' . get_permalink( $post->ID ) . '">Read More</a>';
    $post_excerpt .= $read_more;
    return $post_excerpt;
}

  删除 HTML 标签,从帖子摘录中删除所有 HTML 标签。

add_filter( 'the_excerpt', 'remove_html_tags' );
function remove_html_tags( $post_excerpt ) {
    $post_excerpt = strip_tags( $post_excerpt );
    return $post_excerpt;
}

  添加自定义前缀,在帖子摘录中添加自定义前缀。

add_filter( 'the_excerpt', 'add_custom_prefix' );
function add_custom_prefix( $post_excerpt ) {
    $prefix = 'Summary: ';
    $post_excerpt = $prefix . $post_excerpt;
    return $post_excerpt;
}

  大写摘录,将帖子摘录转换为大写。

add_filter( 'the_excerpt', 'uppercase_excerpt' );
function uppercase_excerpt( $post_excerpt ) {
    $post_excerpt = strtoupper( $post_excerpt );
    return $post_excerpt;
}

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

  推荐:WordPress函数使用手册

5/5 - (1 vote)

晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数the_excerpt过滤显示的帖子摘录

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

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

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