Community Forums › Forums › Archived Forums › Design Tips and Tricks › How to do conditional post excerpt vs. full content based on category?
Tagged: archives, categories, post excerpts
- This topic has 4 replies, 3 voices, and was last updated 11 years ago by tinymachine.
-
AuthorPosts
-
October 4, 2013 at 12:41 pm #65384tinymachineMember
I want the majority of my posts to show as an excerpt on an archive page, but I am using a category called "courses" for a specific purpose and want to always show the full content, even when on an archive page.
Does anyone have a code snippet for this or something similar? I've been through the tutorials and code snippets provided and can't find anything even close. I know how to do this directly in a regular theme, using wordpress functions, but I'm not yet familiar enough with how genesis works to figure this out.
My other alternative is to convert course posts to pages, but I lose a bit of functionality that way that I'd rather not have to lose if possible. Thanks in advance.
http://brooklinetaichi.org/freshwp/category/courses/October 5, 2013 at 12:19 pm #65482David ChuParticipantHi,
This seemed like a pretty little problem, so I gave it a try. I didn't have any luck doing Genesis commands, although one could make a custom loop to replace the Genesis loop without too much difficulty. There are tutorials on that here and there.I did figure out a way to do it using code I had laying around for altering the normal WP excerpt. It's probably taking the long way home, though. 🙂 It can go in your functions.php. The code sets a default word length, which you can obviously change. And you will see that I just made a much longer excerpt for a particular category - this number could be bigger if yu have really long Posts. You will also see that I changed the "read more" text, and you could alter that to taste.
remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'improved_trim_excerpt'); function improved_trim_excerpt($text) { if ( '' == $text ) { $text = get_the_content( get_the_ID() ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text); $text = strip_tags($text, '<p>'); $excerpt_length = 50; if ( in_category('2') ) { $excerpt_length = 1000; } $words = explode(' ', $text, $excerpt_length + 1); $daveLink = ' ...[<a>Read More</a>]'; if (count($words)> $excerpt_length) { array_pop($words); array_push($words, $daveLink); $text = implode(' ', $words); } } return $text; }
Enjoy, Dave
Dave Chu · Custom WordPress Developer – likes collaborating with Designers
October 6, 2013 at 12:38 am #65522Sridhar KatakamParticipantOctober 6, 2013 at 4:50 pm #65593David ChuParticipantYeah, check out Sridhar's stuff... he writes some great material!
Dave Chu · Custom WordPress Developer – likes collaborating with Designers
October 6, 2013 at 4:53 pm #65594tinymachineMemberYes...I just did! Thank you both for the replies....all very helpful
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.