You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:为不同WordPress模板配置不同摘要格式及修复代码

WordPress 自定义两种不同的文章摘要(带/不带“阅读更多”链接)

现有设置

我已经在WordPress 6.1.1的functions.php中添加了以下代码,用来控制默认文章摘要的长度和末尾内容:

function new_excerpt_length($length) {
    return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');

function new_excerpt_more($more) {
    return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

在页面模板1中,我使用<?php echo the_excerpt(); ?>来显示摘要,它能按照上述设置正常工作。

需求

我需要实现第二种摘要场景:在页面模板2中显示无“阅读更多”链接的摘要,同时希望能灵活控制长度等参数。想知道是否可以通过the_excerpt(parameter)传参实现,或者使用wp_trim_excerpt来完成。

尝试的代码及问题

我找到了一段看起来符合需求的自定义函数,但它无法正常工作——无论怎么修改参数,都只输出无链接的摘要:

自定义函数代码

function wpex_get_excerpt( $args = array() ) {

    // Default arguments.
    $defaults = array(
        'post'            => '',
        'length'          => 40,
        'readmore'        => false,
        'readmore_text'   => esc_html__( 'read more', 'text-domain' ),
        'readmore_after'  => '',
        'custom_excerpts' => true,
        'disable_more'    => false,
    );

    // Apply filters to allow child themes mods.
    $args = apply_filters( 'wpex_excerpt_defaults', $defaults );

    // Parse arguments, takes the function arguments and combines them with the defaults.
    $args = wp_parse_args( $args, $defaults );

    // Apply filters to allow child themes mods.
    $args = apply_filters( 'wpex_excerpt_args', $args );

    // Extract arguments to make it easier to use below.
    extract( $args );

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

    // Get the current post id.
    $post_id = $post->ID;

    // Check for custom excerpts.
    if ( $custom_excerpts && has_excerpt( $post_id ) ) {
        $output = $post->post_excerpt;
    }

    // No custom excerpt...so lets generate one.
    else {

        // Create the readmore link.
        $readmore_link = '<a href="' . esc_url( get_permalink( $post_id ) ) . '" class="readmore">' . $readmore_text . $readmore_after . '</a>';

        // Check for more tag and return content if it exists.
        if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
            $output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
        }

        // No more tag defined so generate excerpt using wp_trim_words.
        else {

            // Generate an excerpt from the post content.
            $output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );

            // Add the readmore text to the excerpt if enabled.
            if ( $readmore ) {

                $output .= apply_filters( 'wpex_readmore_link', $readmore_link );

            }

        }

    }

    // Apply filters and return the excerpt.
    return apply_filters( 'wpex_excerpt', $output );

}

调用代码

<?php echo wpex_get_excerpt ( $defaults = array(
    'length'          => 40,
    'readmore'        => true,
    'readmore_text'   => esc_html__( 'read more', 'wpex-boutique' ),
    'custom_excerpts' => true,
) ); ?>

修复后的解决方案

原函数的问题主要集中在参数解析顺序错误和文章对象获取逻辑缺陷,以下是修复后的完整代码:

修复后的自定义摘要函数

function wpex_get_excerpt( $args = array() ) {
    // 默认参数
    $defaults = array(
        'post'            => null,
        'length'          => 40,
        'readmore'        => false,
        'readmore_text'   => esc_html__( '阅读更多', 'your-text-domain' ),
        'readmore_after'  => '',
        'custom_excerpts' => true,
        'disable_more'    => false,
    );

    // 合并用户传入参数与默认值
    $args = wp_parse_args( $args, $defaults );
    // 应用过滤器允许子主题修改参数
    $args = apply_filters( 'wpex_excerpt_args', $args );

    // 提取参数变量
    extract( $args );

    // 获取文章对象:未指定则取当前全局文章
    $post = get_post( $post );
    if ( ! $post ) {
        global $post;
    }
    if ( ! $post ) {
        return '';
    }
    $post_id = $post->ID;

    $output = '';

    // 处理自定义摘要
    if ( $custom_excerpts && has_excerpt( $post_id ) ) {
        $output = $post->post_excerpt;
        // 如果开启readmore,给自定义摘要添加链接
        if ( $readmore ) {
            $readmore_link = '<a href="' . esc_url( get_permalink( $post_id ) ) . '" class="readmore">' . $readmore_text . $readmore_after . '</a>';
            $output .= apply_filters( 'wpex_readmore_link', $readmore_link );
        }
    }
    // 自动生成摘要
    else {
        $readmore_link = '<a href="' . esc_url( get_permalink( $post_id ) ) . '" class="readmore">' . $readmore_text . $readmore_after . '</a>';

        // 处理文章中的<!--more-->标签
        if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) !== false ) {
            if ( $readmore ) {
                $output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
            } else {
                // 不带阅读更多时,只取more标签前的内容
                $content = explode( '<!--more-->', $post->post_content );
                $output = apply_filters( 'the_content', $content[0] );
            }
        }
        // 使用wp_trim_words生成指定长度的摘要
        else {
            $output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );
            if ( $readmore ) {
                $output .= apply_filters( 'wpex_readmore_link', $readmore_link );
            }
        }
    }

    return apply_filters( 'wpex_excerpt', $output );
}

调用示例

  1. 模板2(无“阅读更多”链接的摘要):
<?php echo wpex_get_excerpt( array(
    'length'          => 100, // 自定义长度
    'readmore'        => false, // 关闭阅读更多链接
    'custom_excerpts' => true, // 优先使用自定义摘要
) ); ?>
  1. 带“阅读更多”链接的摘要(替代原the_excerpt):
<?php echo wpex_get_excerpt( array(
    'length'          => 40,
    'readmore'        => true,
    'readmore_text'   => esc_html__( '阅读更多', 'your-text-domain' ),
) ); ?>

修复说明

  • 修正了参数解析顺序:先合并用户参数与默认值,再应用过滤器,确保用户设置不会被覆盖
  • 优化文章对象获取逻辑:未指定文章时自动调用全局文章对象,避免无效调用
  • 补充自定义摘要的阅读更多处理:支持给自定义摘要添加链接(可按需关闭)
  • 完善标签的处理逻辑:关闭阅读更多时,只返回标签前的内容,更符合预期
  • 修复了原代码中的HTML拼接错误,确保链接输出正常

内容的提问来源于stack exchange,提问作者McHunkerson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 21:25:14