$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to just show first line of content|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to just show first line of content

matteradmin9PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far