A
A
Andrey Plyuta2021-11-27 11:12:59
WordPress
Andrey Plyuta, 2021-11-27 11:12:59

How to crop text in wordpress custom post?

In general, I display custom posts "Seminars" in the slider, I need to display the title and excerpt. But the passage needs to be cut to the desired length so that everything looks beautiful. I do the output of the passage through the function Displays the_excerpt()
everything, the entire passage.

I tried to trim it, but nothing happens, the function does not work.

Here is the output code

global $post;

$myposts = get_posts( [
  'posts_per_page' => 6,
  'post_type' => 'seminare',
] );

foreach( $myposts as $post ){
  setup_postdata( $post );
  ?>
                    <div class="event_item">
                        <div class="event_item-wrapper">
                            <strong class="event_item-category">Seminare</strong>
                            <p class="event_item-title"><?php the_title(); ?></p>
                            <p class="event_item-description">
                            <?php the_excerpt();?>
                            </p>

                            <div class="event_item-event-data">
                                <div class="icon-wrap">
                                    <span class="icon-calendar"></span>
                                </div>
                                <span class="text">01.12.2019</span>
                            </div>

                        </div>
                        <a href="<?php the_permalink(); ?>" class="btn active">Weiterlesen</a>
                    </div>


Trimming function:
function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );


Please tell me how to solve the problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2021-11-27
@motor4ikkk

This filter works only with automatically generated excerpts, the excerpts that are entered in the field are not affected by it.
Add a filter like this, it will help

function trim_custom_excerpt( $excerpt ) {

    if ( has_excerpt() ) {
      $excerpt = wp_trim_words( get_the_excerpt(), apply_filters( "excerpt_length", 55 ) );
    }

    return $excerpt;
  }

  add_filter( "the_excerpt", "trim_custom_excerpt", 999 );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question