WordPress.org

Ready to get started?Download WordPress

Forums

How do I display only a few lines of a post? (14 posts)

  1. lonsharkin51
    Member
    Posted 4 years ago #

    Hi. I'm new to WordPress, so forgive me if this is something obvious. I'm using the dkret3 template, and I would like to only display a few lines of each post on my main page, with a "Read more" link for those who want to read the rest. Is this possible? If so, what do I need to do? Thanks for any help you can give me.

  2. lonsharkin51
    Member
    Posted 4 years ago #

    Never mind. I figured it out. It's amazing what you can find if you just search around a little bit.

  3. danball1987
    Member
    Posted 4 years ago #

    how did you do it??

  4. uwiuw
    Member
    Posted 4 years ago #

    i believe this will help

    function the_excerpt_more($text) {
    $url = get_permalink();
    $text = str_replace("[...]", "Read More", $text);
    return $text;
    }
    add_filter('the_excerpt', 'the_excerpt_more');

  5. Samuel B
    moderator
    Posted 4 years ago #

    just use the "more" button above the post editor which will insert this
    <!--more-->
    where you want the post to break

  6. uwiuw
    Member
    Posted 4 years ago #

    ;) hell no, i think i give the wrong advice....

  7. hampusjageland
    Member
    Posted 4 years ago #

    Is there a way of displaying a limited amount of words on the index page and the whole post on the single post page whilst using this code in the function file:

    function my_excerpt_length($text){
    return 19;
    }
    add_filter('excerpt_length', 'my_excerpt_length');
    
    function new_excerpt_more($more) {
    	return '...';
    }
    add_filter('excerpt_more', 'new_excerpt_more');

    and this code in single page:

    <td>
    <?php images('2'); ?>
    </td>
    <td>
    <?php the_excerpt(); ?>
    </td>

    to pull in the images and copy in different locations.

    This won't allow me to show the full excerpt though...

    Any suggestions? I'm so stuck...

  8. hampusjageland
    Member
    Posted 4 years ago #

    Has anyone got any suggestions?

    Cheers,
    H

  9. alexgraham
    Member
    Posted 4 years ago #

    Hampus I think you need to differentiate the_content() and the_excerpt(), I have no idea why you would want to use the_excerpt() on a single.php page, there is better ways to paginate or limit output for those pages. If you want to control the excerpt you can hardcode it by adding the following in your functions.php (example will display 20 words only when the_excerpt() is used):

    function new_excerpt_length($length) {
    	return 20;
    }
    add_filter('excerpt_length', 'new_excerpt_length');
  10. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    you could create a function, which to add to functions.php of your theme, to make a your own excerpt, limited to a certain number of characters, and then reduced to the last full word:

    function intro_text($length) {
    global $post;
    $text = get_the_excerpt($post->ID);
    if (strlen($text) > $length) {
    $text = substr($text,0,strpos($text,' ',$length)) . ' ... <a href="' . get_permalink() . '">[ read more ]</a>'; } ;
    return apply_filters('the_excerpt',$text);
    }

    and in index.php, replace the_excerpt(); with intro_text(150);
    with 150 the desired length of the 'excerpt' in characters, which will be rounded down to the last full word.

  11. hampusjageland
    Member
    Posted 4 years ago #

    alexgraham,
    The reason for using excerpt on the single post pages is that I need to be able to choose where the text will go. If you have a look at http://www.projektinfo.blog and click on one of the posts, that's how I want it to look. Is there a way of doing this without seperating the images and excerpt and inserting them in a table?

    alchymyth,
    Thanks heaps for that code! Unfortunately I can't get it to work properly. I've inserted the code and all, but no difference at all...

    used

    function intro_text($length) {
    global $post;
    $text = get_the_excerpt($post->ID);
    if (strlen($text) > $length) {
    $text = substr($text,0,strpos($text,' ',$length)) . ' ... <a href="' . get_permalink() . '">[ read more ]</a>'; } ;
    return apply_filters('the_excerpt',$text);
    }

    with

    <?php intro_text(150); ?>

    Have no idea why it's not working...
    Thanks though!

    Hampus

  12. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    my bad:
    the line should be: <?php echo intro_text(150); ?>

    hope it helps ;-)

  13. hampusjageland
    Member
    Posted 4 years ago #

    That's perfect!

    Thank you so much for that!

    H

  14. hampusjageland
    Member
    Posted 4 years ago #

    Just wonder if there's a way to make links live in excerpts? And even paragraphs?

Topic Closed

This topic has been closed to new replies.

About this Topic