Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need related post short code to display images from custom fields WordPress

  • SOLVED

Hi

I found the code that allows a current blog post to show related posts based on it's tags:

http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

It works great however instead of a bulleted list with the titles I would like it to display just the thumbnails related to the post (which are pulled in via a custom field in each of these tagged posts)

Can anybody help me?

Thanks
Steve

Answers (1)

2010-10-22

Maor Barazany answers:

I assume you meant to #4 on that page - " Show related posts (uses tags)"

Paste this code instead, I added it inside:


function related_posts_shortcode( $atts ) {

extract(shortcode_atts(array(
'limit' => '5',
), $atts));

global $wpdb, $post, $table_prefix;

if ($post->ID) {

$retval = '
<ul class="related">';

// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(',', $tagsarray);

// Do the query
$q = "
SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p
WHERE tt.taxonomy ='post_tag'
AND tt.term_taxonomy_id = tr.term_taxonomy_id
AND tr.object_id = p.ID
AND tt.term_id IN ($tagslist)
AND p.ID != $post->ID
AND p.post_status = 'publish'
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;";

$related = $wpdb->get_results($q);

if ( $related ) {
foreach($related as $r) {
$meta_field = get_post_meta($r->ID, $your_custom_field_name_here, true);
$retval .= '
<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'"><img src="'.$meta_field.'" alt="'.wptexturize($r->post_title).'" /></a></li>
';
}
} else {
$retval .= '
<li>No related posts found</li>
';
}
$retval .= '</ul>
';
return $retval;
}
return;
}
add_shortcode('related_posts', 'related_posts_shortcode');



Notice the line - $meta_field = get_post_meta($r->ID, $your_custom_field_name_here,


If your custom field key is $image for example, you will write - <strong>$image</strong> instead of - <strong>$your_custom_field_name_here</strong>


Another thing, add to the bottom of youe<strong>style.css</strong> that inside your template folder this code:


ul.related {list-style-type:none;}
ul.related li {list-style-type:none; float:left;}


or just -


ul.related {list-style-type:none;}
ul.related li {list-style-type:none;}


Maor Barazany comments:

A little mistake, instead this line
$meta_field = get_post_meta($r->ID, $your_custom_field_name_here, true);


put this line please


$meta_field = get_post_meta($r->ID, 'your_custom_field_name_here', true);


Where, <strong>your_custom_field_name_here</strong> is your custom field name.


Steven Newman comments:

Ha. I was just about to write that it didn't work when I saw your update. Works great! One question, if I wanted to say that the tags can only come from a certain category, would that be difficult to add (sorry I forgot). Would it be something like:

q="
AND category = Catalog?