ThemeShaper Forums » Thematic

[closed]

Changing content of excerpt conditionally AND editing all loops at once

(4 posts)
  • Started 3 years ago by helgatheviking
  • Latest reply from em hr
  • This topic is resolved

Tags:

  1. i've been using this quite successfully to wrap stuff around the excerpt. and it has the nice benefit of working everywhere there is an excerpt. so of course i have come to a situation where i don't want every excerpt to be the same. (got some stuff going on in a template and need it to be different.

    function all_excerpts_get_more_link($post_excerpt) {
    global $post;
    
    $child_excerpt = '<p>' . $post_excerpt . '</p><p class="readmore"><a href="'. get_permalink($post->ID) . '">' . __('Read More') . '</a></p>'; 
    
    return $child_excerpt;
    	}
    add_filter('wp_trim_excerpt', 'all_excerpts_get_more_link');

    1. is there any way to do this conditionally?

    function all_excerpts_get_more_link($post_excerpt) {
    global $post;
    
    if (is_page()){
    $child_excerpt = '<p>' . $post_excerpt . '</p><p class="readmore"><a href="'. get_permalink($post->ID) . '">' . __('Read More') . '</a></p>';
    }
    else {
    $child_excerpt = 'bacon';
    }
    return $child_excerpt;
    }
    add_filter('wp_trim_excerpt', 'all_excerpts_get_more_link');

    and basically the first condition is never true and i get bacon all the time. which is real life is fab but not so much here. i tried is_page_template, is_home and finally tried is_page. can you just not call a conditional there?

    and since these go together in my head and in my quest to clean my code up a bit...

    2 . i know there are loops for author, tag, search, index and category. is there a way to write 1 loop to rule them all... or do i have to override all 5 separately even if i want them all to behave the same.

    Posted 3 years ago #
  2. No, I don't think that you'll get what you want with that filter but I'm positive that you can do what ever you please with thematic_content_init() and thematic_content()

    With the svn's 0.9.7.4 you could just set up your conditions in a childtheme_override_content_init() Maybe even use more than "full" and "excerpt" for $content values. Why not add an"excerpt-1" or "excerpt-2" etc.

    Then you can adjust the if statement in a childtheme_override_content() to use custom get_the_content args for each type of excerpt.

    http://codex.wordpress.org/Function_Reference/get_the_content

    Have fun with it :)

    Posted 3 years ago #
  3. got it! thanks gene. i crossbred the catch_that_image(), post_thumbnail() and easy_peasy images into 1 mongrel "plugin". and i wanted to call this class in regular excerpts but b/c i had a featured area on the magazine template i am calling it separately there so that i can get different sizes.

    here is how i created 2 styles of excerpt... 1 is the plain excerpt and the other is called 'magazine'. then i only had to remember to switch my the_excerpt() calls in the template back to thematic_content()

    function childtheme_override_content_init() {
    		global $thematic_content_length;
    
    		$content = '';
    		$thematic_content_length = '';
    
    		if (is_page_template('template-magazine.php')) {
    			$content = 'magazine';
    		} elseif (is_home() || is_front_page()) {
    			$content = 'full';
    		} elseif (is_single()) {
    			$content = 'full';
    		} elseif (is_tag()) {
    			$content = 'excerpt';
    		} elseif (is_search()) {
    			$content = 'excerpt';
    		} elseif (is_category()) {
    			$content = 'excerpt';
    		} elseif (is_author()) {
    			$content = 'excerpt';
    		} elseif (is_archive()) {
    			$content = 'excerpt';
    		}
    
    		$thematic_content_length = apply_filters('thematic_content', $content);
    
    	}
    add_action('template_redirect','childtheme_override_content_init');
    
    // Creates the post content 
    
    function childtheme_override_content() {
    		global $thematic_content_length;
    
    		if ( strtolower($thematic_content_length) == 'full' ) {
    			$post = get_the_content(more_text());
    			$post = apply_filters('the_content', $post);
    			$post = str_replace(']]>', ']]>', $post);
    		} elseif ( strtolower($thematic_content_length) == 'magazine') {
    			$post = '' . get_the_excerpt() . '
    <p class="readmore"><a href="'. get_permalink($post->ID) . '">' . __('Read More') . '</a>
    ';
    			$post = apply_filters('thematic_get_excerpt',$post);
    		} elseif ( strtolower($thematic_content_length) == 'excerpt') {
    				if (function_exists( 'the_image' ) AND the_image(array('echo'=>false)) ) {
    
    						$img_args=array( 'width' => '210',
    								'height' => '125',
    								'post_link' => true,
    								'echo' => false);
    
    						$thumb = '<div class="thumb">' . the_image( $img_args) . '</div>';
    				}
    			$post = $thumb . '' . get_the_excerpt() . '
    <p class="readmore"><a href="'. get_permalink($post->ID) . '">' . __('Read More') . '</a>
    ';
    			$post = apply_filters('thematic_get_excerpt',$post);
    		} elseif ( strtolower($thematic_content_length) == 'none') {
    		} else {
    			$post = get_the_content(more_text());
    			$post = apply_filters('the_content', $post);
    			$post = str_replace(']]>', ']]>', $post);
    		}
    		echo apply_filters('thematic_post', $post);
    
    } // end content

    i was banging my head into the wall until i remembered to put the add_action in there. but that also gave me the opportunity to use what you told me a week ago about hooking into template_redirect so that i can use conditionals based on templates.

    Posted 3 years ago #
  4. That's awesome. The overrides make for a pretty cool feature. Thanks for sharing ;)

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.