0

Hoping Im just running into something easy

Here's the scenario, I've created a function called tr_get_post_image_tplt that spits out the post thumbnail (or custom size) for the post being called.

Im calling it as follows from my front-page loop:

<a href="<?php the_permalink(); ?>"<?php post_class('thumbnail span1'); ?> ><?php tr_get_post_image_tplt('thumbnail', 'thumbnail');?><a/>   

I would expect the function to output something like:

<a href="/864/" class="post-864 post type-post status-publish format-quote hentry category-general thumbnail span1"><img src="/wp-content/themes/_tr_roots/img/post-thumb-default.gif" class="post-864 post type-post status-publish format-quote hentry category-general thumbnail" alt="something attachment"/><a/>

But instead Im getting:

<a href="/864/"class="post-864 post type-post status-publish format-quote hentry category-general thumbnail span1">class="post-864 post type-post status-publish format-quote hentry category-general thumbnail"<p><img src="/wp-content/themes/_tr_roots/img/post-thumb-default.gif"  alt="something attachment"/></p>
<a/>    

Note that that the class has slipped out of the <a> tag and extra markup has been added.

I've poured over this for hours now - so Im praying its not some silly typo.

But assuming it isn't a typo can someone explain what's going on and how to avoid it in future?

Here's the function, stored in a functionality plugin(though I've tried it in functions.php with the same results):

function tr_get_post_image_tplt($size='thumbnail', $class='thumbnail', $default='post-thumb-default.gif') {
global $post;

if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), $size );

    $alt_text =  get_the_title(get_post_thumbnail_id());
    $tr_post_thumb = '<img src="' . $image_src[0] . '" ' . post_class($class) . ' alt="' . $alt_text . 'post" />';
    //$tr_post_thumb = the_post_thumbnail($size); isn't flexable enough
}
else{
    $photos = get_children( array(
    'post_parent' => $post->ID, 
    'post_status' => 'inherit', 
    'post_type' => 'attachment', 
    'post_mime_type' => 'image', 
    'order' => 'ASC', 
    'orderby' => 'menu_order ID'
    ) );
    if (!empty($photos)) {
        $photo = array_shift($photos);
        $image_src = wp_get_attachment_image_src($photo->ID, $size);
        $alt_text =  get_the_title($photo->ID);
        $tr_post_thumb = '<img src="' . $image_src[0] . '" ' . post_class($class) . '" alt="' . $alt_text . '" />';
    }
    else {
        // if above fails, spit out a default image
        $alt_text =  get_the_title($post->ID);
        $tr_post_thumb = '<img src="' . get_stylesheet_directory_uri() . '/img/' . $default . '" ' . post_class($class) . ' alt="' . $alt_text . ' attachment"/>';      
    }
}
echo apply_filters('the_content', $tr_post_thumb );
}

Many thanks for sharing your experience!

1 Answer 1

3

Use get_post_class() when you want the values to be returned rather than echod.

4
  • @orionrush : The Codex is your friend, read the Codex :)
    – brasofilo
    May 27, 2012 at 7:17
  • Thanks, but I went to the codex first, Im not being lazy here - honest. I wouldn't waste your time if the answer was evident (to me). My question still stands - In this use case post_class is exactly what I want - I want it to spit out class="some list" so why dosent it output where I've placed it in the markup? - what's gong on? Otherwise we would use get_post_class() in all cases - no? I didn't use get_post_class() because I dont need to further process the array, which is as I understand, what it is for.
    – orionrush
    May 27, 2012 at 11:05
  • Further substituting post_class() with something like 'class="' . implode(get_post_class($class)) . '" dosen't solve the added <p> elements which are being injected into the markup.
    – orionrush
    May 27, 2012 at 16:13
  • Ahh - wordpress.org/support/topic/… Even though the image is already contained in a div, WP dosen't know it - so it automatically wraps it in a <p> to make valid markup. Adding wrapping <div> tags to my function prevents this - though completely superfluous markup IMHO. - I was conflating the two issues as they were appearing concurrently. Still would love to know why post_class() dosen't play nice.
    – orionrush
    May 27, 2012 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.