I am costuming a template. There is a list grabbing the introduction from the first 1-2 paragraphs (all the articles from a category). If I set the excerpt to 295 words, sometimes the list grabs additional words from the next paragraph. I would like to add a Read More tag to stop it. Can someone help me with that part?
<div id="all-div-cabrand-content-stories">
<div class="kids-families-con-cabrand-stories">
<?php echo get_the_post_thumbnail($page->ID, 'thubmnailstorysmall'); ?>
</div>
<div class="kids-con-cabrand-new-stories">
<span>
<?php print substr(get_the_excerpt(),wp_trim_excerpt(),295); ?>
<i><a style="color:#1975D1;float:Right;" class="title" href="<?php the_permalink() ?>" rel="bookmark">Click for Story & Video</a></i>
<br/>
</span>
</div>
</div>
To get a specific length you can use: wp_trim_words function. It has 3 parameters.
get_the_content()
295
''
This means null.Use this:
You can grab the first one or two paragraphs with a regular expression (regexp)
Call the function with
This is a bit tricky because you can not hand over
wp_trim_excerpt()
a text.wp_trim_excerpt()
will simply return the text if one is given.You have to copy and customize the function a bit.
You can use this function:
https://codex.wordpress.org/Function_Reference/wp_trim_words
You can use the built in function
http://codex.wordpress.org/Function_Reference/the_excerpt
To get what you want you need to do two things.
1) Establish a custom excerpt length (in words, not characters), best achieved by following this answer.
2) Just call wp_trim_excerpt(), don’t wrap it inside of substr
Your line of code above is not doing what you are expecting it to do. I believe it’s returning the first 295 characters of the excerpt, but I’m not fully sure of what the php subtr() function is going to do when you pass it a string as the second argument when it’s expecting an integer.