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!