Excerpt length in category archive and search pages
-
Hi, can someone help me with the php code needed to limit the number of characters in excerpt view before the “continue reading” button is displayed?
Basically, when someone clicks on category archives or performs a search, and the resulting excerpts are displayed…I want the continue reading button to be displayed after 150 characters. How?
THANKS!
You can use this function in your child theme functions.php file:
function custom_excerpt_length($length) {
return 150; // Change this to whatever you want.
add_filter('excerpt_length', 'custom_excerpt_length');
}Alternatively, you can use this plugin for even more control:
Hey Josh, as always, thanks for your reply. I pasted your code into my child theme functions.php….but it didn’t do anything. Here is my file:
<?php
function searchfilter($query) {
if ($query->is_search) {
$query->set('post_type',array('post'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
// Change some strings
function graphene_filter_readmore( $translated, $original, $domain ) {
$strings = array(
'Continue reading »' => 'CONTINUE READING!!',
'Read the rest of this entry »' => 'CONTINUE READING!!',
);
if ( ! empty( $strings[$original] ) ) {
$translations = &get_translations_for_domain( $domain );
$translated = $translations->translate( $strings[$original] );
}
return $translated;
}
add_filter( 'gettext', 'graphene_filter_readmore', 10, 3 );
function custom_excerpt_length($length) {
return 40; // Change this to whatever you want.
add_filter('excerpt_length', 'custom_excerpt_length');
}
?>Did I do something wrong? I changed the value to 40…but when I click on my category archives….its still the same as before….it still goes with the default length?
Any thoughts?
Admin
There’s a mistake in the code. Instead of this:
function custom_excerpt_length($length) {
return 40; // Change this to whatever you want.
add_filter('excerpt_length', 'custom_excerpt_length');
}it should be this:
function custom_excerpt_length($length) {
return 40; // Change this to whatever you want.
}
add_filter('excerpt_length', 'custom_excerpt_length');Oops. Thanks Syahir.
Admin
40 words.
This still does not work. I pasted your code Syahir in my child theme functions.php file….but still no change. It still adds the continue reading button automatically. I changed return to 20 and expected to see each excerpt 20 words long and then the continue reading button…but no change….any ideas?
Admin
Try inserting this instead:
function your_custom_excerpt_length(){
graphene_set_excerpt_length( 20 );
}
add_action( 'after_setup_theme', 'your_custom_excerpt_length' );
Viewing 10 posts - 1 through 10 (of 16 total)
- 1
- 2
- You must be logged in to reply to this topic.