How to just show first line of content

admin2025-06-06  17

is it possible to show only the first line of content <?php the_content(); ?> or alternative only content between <b>text</b>?

is it possible to show only the first line of content <?php the_content(); ?> or alternative only content between <b>text</b>?

Share Improve this question asked Nov 21, 2018 at 15:52 joloshopjoloshop 211 silver badge8 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

you could also do something like:

function rt_before_after($content) {

 $replace = "</b>";
 $shortcontent = strstr($content, $replace, true).$replace;

 if ($shortcontent === false) $shortcontent = $content;

    return $shortcontent;
}
add_filter('the_content', 'rt_before_after');

It should look for the first </b> in your content and return everything before that. it then adds the </b> back. The function takes that string and replaces your content.

What you can do is work with this the_excerpt() change this instead of the_content();

And give the amount of words that you want, add this in functions.php after you add the_excerpt();

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

More info here: https://developer.wordpress/reference/functions/the_excerpt/

After that force the first line, using this for example.

.post p {
    width: 100px;
    white-space:nowrap;
    overflow:hidden;
}

add the width of the content.(even if its responsive just change the css as you want).

Found a solution! I use this code:

    function awesome_excerpt($text, $raw_excerpt) {
    if( ! $raw_excerpt ) {
        $content = apply_filters( 'the_content', get_the_content() );
        $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
    }
        return $text;
}
add_filter( 'wp_trim_excerpt', 'awesome_excerpt', 10, 2 );

and than use <?php the_excerpt(); ?>

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749161734a316886.html

最新回复(0)