ThemeShaper Forums » Thematic

[closed]

Adding thumbnails to posts on home page

(10 posts)
  • Started 3 years ago by erikvlie
  • Latest reply from vifa
  • This topic is not resolved
  1. Hi,

    I want to have thumbnails in front of my post text, but no other metadata.

    I removed the child theme's (Powerblog) own metadata with the following code:

    function childtheme_postheader() {
        global $post; 
    
        if (is_page() || is_404() || is_home()) { ?>
          <h2 class="entry-title"><a href="<?php the_permalink() ?>" title="<?php printf(__('Permalink to %s', 'thematic'), wp_specialchars(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h2>
      <?php } else { ?>
        <h1 class="entry-title"><a href="<?php the_permalink() ?>" title="<?php printf(__('Permalink to %s', 'thematic'), wp_specialchars(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h1>
    				<?php if ($post->post_type == 'post') { ?>
    		<div class="entry-meta">
              	<span class="author vcard"><span class="icon"><img src="/media/By.png" alt="" /></span> <?php _e(' ', 'thematic') ?><a class="url fn n" href="<?php get_author_link(true, $authordata->ID, $authordata->user_nicename); ?>" title="<?php __('View all posts by ', 'thematic') . the_author(); ?>"><?php the_author() ?></a></span>
    			<span class="meta-sep"> | </span>
    			<span class="entry-date"><abbr class="published" title="<?php get_the_time('Y-m-d\TH:i:sO'); ?>"><?php the_time('F jS, Y') ?></abbr></span>
    		</div><!-- .entry-meta -->
    		<?php } ?>
        <?php }
    }
    add_filter ('thematic_postheader', 'childtheme_postheader');

    But now I must add the thumbnail code, and I don't know how. I gave it a try but what follows is a total mess and although I don't understand why, I already knew by instinct this wasn't going to work:

    //Post thumbnail support
    	add_theme_support('post-thumbnails');
    
    	function post_thumbnails($postmeta = get_the_post_thumbnail() ) {
    
    $thumb = get_post_meta($post->ID, 'Thumbnail', $single = true);
    	$thumb_class = get_post_meta($post->ID, 'Thumbnail Class', $single = true);
    	$thumb_alt = get_post_meta($post->ID, 'Thumbnail Alt', $single = true);
    	if($thumb !== '') { ?>
    	<p>
    	<img src="<?php echo $thumb; ?>"
    	class="<?php if($thumb_class !== '') { echo $thumb_class; } else { echo "left"; } ?>"
    	alt="<?php if($thumb_alt !== '') { echo $thumb_alt; } else { echo the_title(); } ?>"
    	/>
    	</p>
    <?php } // end if statement
    
    // if there's not a thumbnail
    else { echo ''; } ?>
    <? php	}
    add_action('init', 'post_thumbnails');

    Is there anyone who can tolerate this level of programming/logic ignorance and help me get this right?

    Posted 3 years ago #
  2. I know the code above is unbelievably stupid, and I admit I am retarded when it comes to programming. Which is why I would like a helping hand... And that helping hand would be highly valued!

    Posted 3 years ago #
  3. this might help: http://justintadlock.com/archives/2009/11/16/everything-you-need-to-know-about-wordpress-2-9s-post-image-feature

    wp now does thumbs natively so unless you have an old site w/ lots of thumbs in the custom fields you can probably scratch that.

    here is some code i am using to get thumbs everywhere i have excerpts

    add_theme_support( 'post-thumbnails' );
    
    //wrap thumbs in link to post
    
    function my_post_image_html( $html, $post_id, $post_image_id ) {
                  $html = '<div class="thumbnail"><a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html .  '<span class="linktopost">' . get_the_title($post_id) . '</span></a></div>';
                  return $html;
    }
    
    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
    
    //use excerpts on home page
    
    function child_content($content) {
                  if ( is_home() ) {
                                $content = 'excerpt';
                  }
    return $content;
    }
    
    add_filter('thematic_content', 'child_content');

    // add thumbnails, read more links and wrap excerpt in p tag

    function all_excerpts_get_more_link($post_excerpt) {

    if ( has_post_thumbnail() ) {
    $child_excerpt = the_post_thumbnail( 'thumbnail' ) . '<p>' . $post_excerpt . '</p>' . '<p class="readmore">ID) . '">' . 'Continue Reading &raquo' . '</p>';

    } else {
    $child_excerpt = '<p>' . $post_excerpt . '</p>' . '<p class="readmore">ID) . '">' . 'Continue Reading &raquo' . '</p>';
    }
    return $child_excerpt;
    }

    add_filter('wp_trim_excerpt', 'all_excerpts_get_more_link');

    `

    you could possibly wrap my all_excerpts_get_more_link function in some conditionals if you only want it to apply to the home blog page. as is, it changes the excerpt for every spot on my site that uses excerpts.

    Posted 3 years ago #
  4. Hi Helga,

    Again thanks for helping me here. I am using the Thematic version that works with WordPress 3.0 (0.9.7.x) and I first missed it, but it appears this version has post_thumbnail support by default.

    All I need to do now is add the thumbnails in the right place where I want them. That's where your code comes in as a great starting point!

    Thanks also for the Justin Tadlock article. I Googled for post_thumbnail yesterday, but this story didn't come up in my search, or I missed it. I saved his story as a Web Archive in Yojimbo, my PIM. I think I'll bookmark his blog too.

    Posted 3 years ago #
  5. Unfortunately, the all_excerpts_get_more_link function doesn't work on my system. When I use it, all pages are blank...

    I also don't want to trim my excerpts. I thought I had found a fix, but it also doesn't work...

    function thumbnail_show() {
    	if(has_post_thumbnail()) {
    	'<p class="thumbnail">' . the_post_thumbnail();
    } else {
    	echo '';
    }
    }
    add_filter('thematic_post', 'thumbnail_show');

    This makes all pages go blank too...

    Posted 3 years ago #
  6. i see my code didn't get displayed properly. did you get a parse error or do you get page titles w/ blank excerpts. or do you get the white screen of death? if the latter, i must be giving out code relevant to the latest stable dev release again. my bad. i didn't know b/c i unearthed the code here and it has been by far the easiest snippet for adding thumbnails to excerpts. maybe try thematic 0.9.7.3 http://developing.thematic4you.com/thematic-development-release/ or maybe try changing all instances of $child_excerpt to $post_excerpt and see if that makes a difference.

    it doesn't trim the excerpt at all (that is another function)... it just happens to be the name of the function we're hooking in to. sometimes i don't test before i post, but i can verify that this works for me both locally and live on the last site i worked on.

    // add thumbnails, read more links and wrap excerpt in p tag
    
    function all_excerpts_get_more_link($post_excerpt) {
    	if ( has_post_thumbnail() ) {
    		$child_excerpt =  the_post_thumbnail( 'thumbnail' ) . '' . $post_excerpt . '
    ' . '<p class="readmore"><a href="'. get_permalink($post->ID) . '">' . 'Continue Reading &raquo' . '</a>
    ';
            }
        else {
            $child_excerpt = '' . $post_excerpt . '
    ' . '<p class="readmore"><a href="'. get_permalink($post->ID) . '">' . 'Continue Reading &raquo' . '</a>
    ';
            }
        return $child_excerpt;
    }
    
    add_filter('wp_trim_excerpt', 'all_excerpts_get_more_link');

    also there is an extra <span> in the my_post_image_html that i was using for other purposes, but isn't necessary.

    Posted 3 years ago #
  7. I got the white screen of death... Even when I changed only what I thought was formatting in your last code, I got that screen. I'm not always this stupid; sometimes my logic actually works, but more often it doesn't; either that or I'm overlooking typos (is that possible about a dozen times?).

    Anyway, all I have to do now is format the thumbnail so it looks the way I want it to. A BIG, HUGE thanks!

    Posted 3 years ago #
  8. Hi Helga,

    I can not figure out how to wrap a href around my thumbs. I use your script and below is my attempt.

    $child_excerpt = '<a href="'. get_permalink($post->ID) . '">' . the_post_thumbnail( 'thumbnail' ) . '</a>'

    Do you have any clue?

    Many thanks in advance!

    Posted 3 years ago #
  9. see the link above at justin tadlock's site. he shows how to change the html of the thumbnail

    Posted 3 years ago #
  10. Many thanks for your help Helga. Now it works. :-)

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.