How to do conditional post excerpt vs. full content based on category?

Community Forums Forums Design Tips and Tricks How to do conditional post excerpt vs. full content based on category?

This topic is: not resolved

This topic contains 4 replies, has 3 voices, and was last updated by  tinymachine 9 months, 3 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #65384

    tinymachine
    Participant
    Post count: 1

    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/
    #65482

    David Chu
    Participant
    Post count: 1329

    Hi,
    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.

    [php]
    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 = ‘  …[Read More]‘;
    if (count($words)> $excerpt_length) {
    array_pop($words);
    array_push($words, $daveLink);
    $text = implode(‘ ‘, $words);
    }
    }
    return $text;
    }
    [/php]

    Enjoy, Dave


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

    #65522

    Sridhar Katakam
    Participant
    Post count: 925
    #65593

    David Chu
    Participant
    Post count: 1329

    Yeah, check out Sridhar’s stuff… he writes some great material!


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

    #65594

    tinymachine
    Participant
    Post count: 1

    Yes…I just did! Thank you both for the replies….all very helpful

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.