繁体   English   中英

将 excerpt_length 添加到 Understrap WordPress 主题

[英]Add excerpt_length to the Understrap WordPress theme

我正在尝试将长度为 50 的摘录添加到以下 Understap 主题代码中。 https://github.com/understrap/understrap/blob/main/inc/extras.php

具体代码为:

add_filter( 'wp_trim_excerpt', 'understrap_all_excerpts_get_more_link' );

if ( ! function_exists( 'understrap_all_excerpts_get_more_link' ) ) {
    /**
     * Adds a custom read more link to all excerpts, manually or automatically generated
     *
     * @param string $post_excerpt Posts's excerpt.
     *
     * @return string
     */
    function understrap_all_excerpts_get_more_link( $post_excerpt ) {
        if ( is_admin() || ! get_the_ID() ) {
            return $post_excerpt;
        }

        $permalink = esc_url( get_permalink( (int) get_the_ID() ) ); // @phpstan-ignore-line -- post exists

        return $post_excerpt . ' [...]<p><a class="btn btn-secondary understrap-read-more-link" href="' . $permalink . '">' . __(
            'Read More...',
            'understrap'
        ) . '<span class="screen-reader-text"> from ' . get_the_title( get_the_ID() ) . '</span></a></p>';

    }
}

希望对 WordPress 非常了解的人可以看到一种将 excerpt_length 集成到其中的便捷方法。 https://developer.wordpress.org/reference/hooks/excerpt_length/

谢谢

add_filter( 'excerpt_length', 'understrap_custom_excerpt_length', 999 );
function understrap_custom_excerpt_length( $length ) {
    return 50;
}

您可以在add_filter( 'wp_trim_excerpt', 'understrap_all_excerpts_get_more_link' ); extras.php 文件中的行。

祝你好运 !

幸运的是,这对我有用:

function custom_excerpt_length($excerpt) {
    if (has_excerpt()) {
        $excerpt = wp_trim_words(get_the_excerpt(), apply_filters("excerpt_length", 30));
    }
    return $excerpt;
}
add_filter("the_excerpt", "custom_excerpt_length", 999);

https://wordpress.stackexchange.com/questions/139953/excerpt-length-not-working偶然发现了它

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM