Community Forums › Forums › Archived Forums › General Discussion › Custom excerpt length
Tagged: Excerpt
- This topic has 4 replies, 3 voices, and was last updated 7 years, 10 months ago by tanyaslogos.
-
AuthorPosts
-
September 22, 2016 at 7:39 am #193520studionooblyMember
How can I set up an excerpt on posts that has a limit of say 35 words/228 characters and then shows a customised 'Read More' link at the end?
I have tried the following:
add_filter( 'excerpt_length', 'change_excerpt_length' ); function change_excerpt_length($length) { return 35; }
But if there are less than 35 words lets say four words then no button shows up?
I have also tried this:
add_filter('excerpt_more', 'get_read_more_link'); add_filter( 'the_content_more_link', 'get_read_more_link' ); function get_read_more_link() { return '<a class="button archive-button" href="' . get_permalink($post->ID) . '">Continue Reading <i class="fa fa-angle-right" aria-hidden="true"></i></span></a>'; }
But again if there's less than four or five words no button shows up? I have checked Genesis theme settings there is a show excerpt option but no option to control the amount of words or characters.
Lastly I also tried this:
function wpm_manual_excerpt_read_more_link($excerpt) { $excerpt_more = ''; if (has_excerpt() && ! is_attachment() && get_post_type() == 'post') { $excerpt_more = ' <a href="' . get_permalink() . '">[Continue reading] <span class="screen-reader-text">' . get_the_title() . '</span></a>'; } return $excerpt . $excerpt_more; }
But no button...
Thanks
http://localhostSeptember 22, 2016 at 10:56 am #193536JessMemberThis is what I add to my functions.php file to add 'Read More' text to the ends of post excerpts (change the bold text to whatever you'd like):
// Modify The Read More Link Text
function new_excerpt_more($more) {
return ' ... [Read More]';
}
add_filter('excerpt_more', 'new_excerpt_more');And this is what I add to limit the length of those excerpts (change the bolded number to the number of characters you'd like):
// Change length of post excerpt
function custom_excerpt_length( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
September 22, 2016 at 10:58 am #193537JessMemberAh -- sorry! I forgot to wrap that first chunk of code it the right tags so this site killed some of it. Here's what it should have said:
// Modify The Read More Link Text function new_excerpt_more($more) { return ' <a class="read-more" href="' . get_permalink( get_the_ID() ) . '">... [Read More]</a>'; } add_filter('excerpt_more', 'new_excerpt_more');
September 23, 2016 at 4:05 am #193576studionooblyMemberThanks Jess,
I've tried that you can see it in the second snippet of code in my question.
Fortunately and after much searching I found that this works when I put it into the category templates.
echo substr(get_the_excerpt(), 0,350).' [...]'; echo '<a class="button archive-button" href="'. get_permalink( get_the_ID() ).'">Continue Reading </a>';
you can view the full code here at stackoverflow
January 22, 2017 at 3:09 pm #199804tanyaslogosMemberThank you Jess!!!! I kept trying the code listed here http://my.studiopress.com/snippets/post-excerpts/#content-more-link and it wouldn't work but your code did the job!
Thank you so very much! You are the BEST!
-
AuthorPosts
- The topic ‘Custom excerpt length’ is closed to new replies.