ThemeShaper Forums » Thematic

[closed]

using more tags preferentially with excerpt fallback

(3 posts)
  • Started 3 years ago by helgatheviking
  • Latest reply from D.Lo
  • This topic is resolved
  1. thematic_content defines the way that full content pages and excerpts look in thematic. has anybody ever created a different type? i've done that before, but this time i am specifically looking to create a type that first looks for full content before a <more> tag and then failing that autogenerate an excerpt. any thoughts?

    so for example, i'd first define that i want to use this new 'type' on category pages

    function childtheme_content($content) {
    	if (is_category()) {
    		$content= 'more';
    	}
    	return $content;
    }
    
    add_filter('thematic_content', 'childtheme_content');

    and then hypothetically something like

    `
    function childtheme_more($post){

    /* if (has <!--more--> tag) {
    $post = content up until more tag;
    } else {
    $post = get_the_excerpt();
    }
    */

    return $post;
    }
    add_filter('thematic_content','childtheme_more');

    Posted 3 years ago #
  2. solved this for anyone who cares to know. the first part above is correct... you must filter thematic_content and tell it where to use this new "type". and now to filter thematic_post

    function childtheme_excerpts($content) { //normally i'd call $post here as that is what thematic uses,  but that caused problems with the next line
    	global $thematic_content_length, $post; //needed to declare the standard $post object
    
    		if ( strtolower($thematic_content_length) == 'more' ) {
    
    			if ( preg_match('/<!--more(.*?)?-->/', $post->post_content ) ) {  //here's where i needed to the $post object. didn't seem to work at all with get_the_content()
    				$content = get_the_content(more_text());
    				$content = apply_filters('the_content', $content);
    				$content = str_replace(']]>', ']]>', $content);
    
    			} else {
    				$content = '';
    				$content .= get_the_excerpt();
    				$content = apply_filters('the_excerpt',$content);
    				if ( apply_filters( 'thematic_post_thumbs', TRUE) ) {
    					$content_title = get_the_title();
    					$size = apply_filters( 'thematic_post_thumb_size' , array(100,100) );
    					$attr = apply_filters( 'thematic_post_thumb_attr', array('title'	=> 'Permalink to ' . $post_title) );
    					if ( has_post_thumbnail() ) {
    						$content = '<a class="entry-thumb" href="' . get_permalink() . '" title="Permalink to ' . get_the_title() . '" >' . get_the_post_thumbnail(get_the_ID(), $size, $attr) . '</a>' . $content;
    					}
    				}
    			}
    
    		} 
    
    	return $content; //send it back to the function. by using $content here i don't monkey with the $post object which was breaking the 'excerpt' and 'full' types. thematic_post will take this variable as the $post variable there and all will be well (as far as i can tell)
    
    }
    
    add_filter('thematic_post', 'childtheme_excerpts');

    i've tweaked it out a little for my own needs, but the above is the heart of what i was going for. if there is a more tag present it will display the full content up to said tag... if there is no more tag it will present the standard thematic excerpt.

    enjoy!

    Posted 3 years ago #
  3. D.Lo
    Member

    I found this incredibly helpful and wanted to give you props. I've looked through the forum for days and this code ended up being the basis for how I customized my child theme. The result is home/category pages that show the teaser (i.e. up to the more tag if present), and then an automatic excerpt if there is no teaser. I also added a second customization to replace the ellipsis after an auto-excerpt with a "read more" link.

    Here's my code in functions.php


    <?php //Automatic use of teaser or excerpt for home/category pages. Logic from http://forums.themeshaper.com/topic/using-more-tags-preferentially-with-excerpt-fallback
    function childtheme_excerpts($content) { //normally i'd call $post here as that is what thematic uses, but that caused problems with the next line
    global $thematic_content_length, $post; //needed to declare the standard $post object
    if (is_home() || is_front_page()|| is_category()) {
    if ( preg_match('/<!--more(.*?)?-->/', $post->post_content ) ) { //here's where i needed to the $post object. didn't seem to work at all with get_the_content()
    $content = get_the_content(more_text());
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);

    } else {
    $content = '';
    $content .= get_the_excerpt();
    $content = apply_filters('the_excerpt',$content);
    }
    }
    return $content; //send it back to the function. by using $content here i don't monkey with the $post object which was breaking the 'excerpt' and 'full' types. thematic_post will take this variable as the $post variable there and all will be well (as far as i can tell)
    }
    add_filter('thematic_post', 'childtheme_excerpts');
    ?>
    <?php //If the WordPress automatic "excerpt" is displayed, replace the elipsis with a Read More link
    function excerpt_ellipse($text) {
    return str_replace('[...]', '...<p>Read More ยป</p>', $text); }
    add_filter('get_the_excerpt', 'excerpt_ellipse');
    ?>

    Thanks for putting this out there Helga!

    Posted 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.