Display only 2 lines of text on each search result

I would like to edit some styling in the site to have only two lines of text displayed on each of the search results.

Could the code suggested here be adapted to affect only the search results: https://wordpress.stackexchange.com/questions/70913/how-can-i-limit-the-character-length-in-excerpt/70914#70914?

Many thanks in advance.

  • Ash
    • WordPress Hacker

    Hello mohamed

    You can’t make it based on the number of lines but on the number of characters. For example, if you want to show 100 characters, you can try this code:

    add_filter( 'the_content', function( $content ) {
    if( ! is_search() ) return;

    return substr( $content, 0, 100 );
    } );

    You can change 100 to any number.

    You can use this code in your child theme’s functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!



Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.