简体   繁体   English

将 excerpt_length 添加到 Understrap WordPress 主题

[英]Add excerpt_length to the Understrap WordPress theme

I am trying to add an excerpt length of eg 50 to the following Understap theme code.我正在尝试将长度为 50 的摘录添加到以下 Understap 主题代码中。 https://github.com/understrap/understrap/blob/main/inc/extras.php https://github.com/understrap/understrap/blob/main/inc/extras.php

The specific code is:具体代码为:

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>';

    }
}

Hoping someone very knowledgeable of WordPress can see a convenient way of integrating excerpt_length into it.希望对 WordPress 非常了解的人可以看到一种将 excerpt_length 集成到其中的便捷方法。 https://developer.wordpress.org/reference/hooks/excerpt_length/ https://developer.wordpress.org/reference/hooks/excerpt_length/

Thanks谢谢

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

You can add this code before the add_filter( 'wp_trim_excerpt', 'understrap_all_excerpts_get_more_link' );您可以在add_filter( 'wp_trim_excerpt', 'understrap_all_excerpts_get_more_link' ); line in your extras.php file. extras.php 文件中的行。

Good luck !祝你好运 !

This luckily works for me:幸运的是,这对我有用:

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

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

Thanks谢谢

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

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