WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Difficulty limiting post excerpts (20 posts)

  1. Graycode
    Member
    Posted 1 year ago #

    Hi All,

    I have looked for a solution to my problem and although i have seen code such as adding this to functions.php:

    function limit_words($string, $word_limit) {
    
    	// creates an array of words from $string (this will be our excerpt)
    	// explode divides the excerpt up by using a space character
    
    	$words = explode(' ', $string);
    
    	// this next bit chops the $words array and sticks it back together
    	// starting at the first word '0' and ending at the $word_limit
    	// the $word_limit which is passed in the function will be the number
    	// of words we want to use
    	// implode glues the chopped up array back together using a space character
    
    if (count($words) > $word_limit ) {$end = '…';}
    
    return implode(' ', array_slice($words, 0, $word_limit)). $end;
    }

    and this in my blog.php: ` <?php echo limit_words(get_the_excerpt(), '10'); ?>
    `
    The link is http://test.akeytodesign.com/blog/

    Help is much appreciated!

    Thanks

  2. Aaron
    Member
    Posted 1 year ago #

    I see a few ways that work on this thread.

  3. Graycode
    Member
    Posted 1 year ago #

    Thanks Aaron, I did see that but it seems to only take a snippet of the excerpt off. I wonder if it is the placement of the code but this is how the code looks after I've put it in.

    <div id="blogpage">
    	<?php query_posts('posts_per_page=2'); ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
            <div class="post-block-blog">
    
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
           <p class="post-date"><?php the_time('j M Y'); ?></p>
    
            <?php the_content(); ?>
    
    		<?php
              $excerpt = get_the_excerpt();
              echo string_limit_words($excerpt,25);
            ?>
    
          <p class="readmore"> <a href="<?php the_permalink(); ?>" title="Continue reading this article">Read more</a></p>
            </div>
    		<?php endwhile; ?>

    I have even reduced and increased the number just to see where I could be going wrong... but still cant figure it out! :s

  4. Aaron
    Member
    Posted 1 year ago #

    What exactly are you trying to accomplish?

  5. Graycode
    Member
    Posted 1 year ago #

    I'm trying to accomplish having just a preview of my blog posts visible on the blog page.

    See this link- http://test.akeytodesign.com/wp-content/uploads/2013/01/Yato-Blog-excerpt.jpg

    Right now, it's displaying the entire blog post and I want it to show as above.

  6. Aaron
    Member
    Posted 1 year ago #

    This is how I always do it.

    Find

    <?php the_content(); ?>

    change to

    <?php the_excerpt(); ?>

  7. Graycode
    Member
    Posted 1 year ago #

    So to clarify, would you write it as the link demonstrates but just changing the above?

    I have an image in my content so would this be affected?

  8. Aaron
    Member
    Posted 1 year ago #

    Yes just change the word content to excerpt.

    You will not see the image in the excerpt. They would need to click through to the actual page/post to see it.

  9. Graycode
    Member
    Posted 1 year ago #

    Thank you. Yes, I changed it and although it looks good, that image needs to display as I showed in the link.

    I'd need it to show the image associated with the post and an excerpt underneath.

  10. Aaron
    Member
    Posted 1 year ago #

    Above the excerpt code add this.

    <?php if ( has_post_thumbnail() ) {
      the_post_thumbnail();
    } ?>

    Now go back in your post and on the right side set the Featured Image. What this code does is display whatever image is set as the Featured Image in the post.

  11. Graycode
    Member
    Posted 1 year ago #

    Awesome! Works brilliantly, thanks Aaron. :)

    Lastly, can I amend how much of that excerpt is displayed?

  12. Aaron
    Member
    Posted 1 year ago #

    Go in and edit your post and where you want it to cutoff put the below code.

    <!--more-->

    There is also a button for that code in the HTML editor.

  13. Graycode
    Member
    Posted 1 year ago #

    It works but not accurately.

    I used the Visual editor to place that code in but its like it cuts it off but adds the rest to the end lol.

    I've added several spaces where the space is meant to be so you can see what i mean. http://test.akeytodesign.com/blog/

    If its an unnecessary hassle, I can leave it but I'd like to be as accurate to that image i showed you as possible.

    You've been so helpful thank you.

  14. Anoodles
    Member
    Posted 1 year ago #

    hey guys i ve been having difficulty to find the excerpt part because all i can find is 'content'.
    IT changed really sudden when i changed the backround pic in simple catch theme and from full post it only shows an excerpt... i ve tried to find it but i cant... please help!!!

  15. Anoodles
    Member
    Posted 1 year ago #

    oh the site is this if anyone would like to help me!
    http://www.tincturetales.com/

  16. Aaron
    Member
    Posted 1 year ago #

    Anoodles,

    You would need to start your own topic for what problems you are having, not jump in on someone elses.

  17. esmi
    Forum Moderator
    Posted 1 year ago #

    @Anoodles: It is impolite to interrupt another poster's ongoing thread with a question of your own. It causes significant problems for the forum's volunteers and prevents us from being able to track issues by topic. Please post your own topic.

  18. Anoodles
    Member
    Posted 1 year ago #

    I'm sorry guys i thought that because i wanted the exact opposite on the post thing that it would be considered relevant, that's why i asked here... I won't do it again :(

  19. Graycode
    Member
    Posted 1 year ago #

    Thanks...

    Does anyone have a solution to my last point so I can state this as 'resolved'?

    Can you state how much of an excerpt you wish to display?

  20. Graycode
    Member
    Posted 1 year ago #

    Actually I have worked it out thanks to http://artarmstrong.com/blog/2011/01/02/how-to-modify-the_excerpt-in-wordpress/

    Put this in your functions.php and modify the 150 according to how much of the excerpt you wish to display.

    //Modify the length of the_excerpt
    function new_excerpt_length($length) {
    	return 150;
    }
    add_filter('excerpt_length', 'new_excerpt_length');
    
    //Modify the "Read More" link of the_excerpt
    function new_excerpt_more($more) {
    	$new = str_replace("Continue reading ", "Read More", $more);
    	$new = str_replace('<span class="meta-nav">→</span>', "", $new);
    	return $new;
    }
    add_filter('excerpt_more', 'new_excerpt_more');

Topic Closed

This topic has been closed to new replies.

About this Topic