最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - Which file handles the block latest posts, I want to examine excerpt handling

programmeradmin4浏览0评论

I'm using the latest version of wordpress and theme twentytwenty.

When using the latest post block in the block editor, I am showing excerpts.

It appears that using [more] to handle the length of automatic excerpts is not working properly.

I tried fiddling with the max number of words setting..

I expect to be able to set the max words to, say 25, and in some post "B" put the [more] marker in at word 20, that I would get an excerpt of 25 words in post A, and 20 in post B.

That doesn't work, so I try setting max words to 100, still not respecting the [more] marker.

I'm a pretty good hacker, Where do I start? Is this a known problem and harder than I might expect?

Thanks!

I'm using the latest version of wordpress and theme twentytwenty.

When using the latest post block in the block editor, I am showing excerpts.

It appears that using [more] to handle the length of automatic excerpts is not working properly.

I tried fiddling with the max number of words setting..

I expect to be able to set the max words to, say 25, and in some post "B" put the [more] marker in at word 20, that I would get an excerpt of 25 words in post A, and 20 in post B.

That doesn't work, so I try setting max words to 100, still not respecting the [more] marker.

I'm a pretty good hacker, Where do I start? Is this a known problem and harder than I might expect?

Thanks!

Share Improve this question asked Mar 10, 2020 at 22:58 Rob ForeeRob Foree 1134 bronze badges 1
  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt Commented Mar 10, 2020 at 23:07
Add a comment  | 

2 Answers 2

Reset to default 1

Gutenberg blocks are not stored in the Wordpress files in a readable or debuggable format. Instead, they have their own separate git repos.

Here is the source for the Latest Posts Block: https://github/WordPress/gutenberg/tree/master/packages/block-library/src/latest-posts

Something you might try, I found this solution years ago and it's worked well - it does more than just allow overriding the number of words limitation, it also allows for some basic styles so that if your excerpt contains bolded words those will show in the excerpt.

// Improves the look of the excerpt, more words, allows bolding

function improved_trim_excerpt($text) {
$raw_excerpt = $text;

$excerpt_more = '&hellip;<a class="more-link" href="'. esc_url( get_permalink() ) . '" title="' . esc_html__( 'Continue reading', 'wp-answers' ) . ' &lsquo;' . get_the_title() . '&rsquo;">' . wp_kses( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'wp-answers' ), array( 'span' => array( 
        'class' => array() ) ) ) . '</a>';    

if ( '' == $text ) {
    $text = get_the_content('');
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace('\]\]\>', ']]&gt;', $text);
    $text = strip_tags($text, '<b><strong><del><em>');
    $excerpt_length = apply_filters('excerpt_length', 55);//change 55 to whatever word limit you want
    $newexcerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_more);
    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
        array_pop($words);
        $text = implode(' ', $words);
        $text = $text . $newexcerpt_more;
        $text = force_balance_tags( $text );
    } else {
        $text = implode(' ', $words);
        $text = force_balance_tags( $text );
    }
}
return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');

This would go in your (hopefully child) theme's functions.php file or a custom plugin file.....

发布评论

评论列表(0)

  1. 暂无评论