Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

For Microkids related post plugin He said

"Using the get_post() function you could get any data you need from the related posts."

and he posted this snippet of code.

$related_posts = MRP_get_related_posts( $post_id );

I'm not exactly sure how to use the get_post feature for this situation. I would like to display the titles and thumbnail for each post in that array. Any help would be appreciated.

share|improve this question

3 Answers 3

up vote 0 down vote accepted

possibly:

$related_posts = MRP_get_related_posts( $post->ID );   
    if( $related_posts ) foreach( $related_posts as $key=>$value ) { 
        //only holds the following information:
        //echo $key; //the related_post_id  
        //echo $value;  //the related post title
        echo get_the_title($key);     
        echo get_the_post_thumbnail($key);   
    }

(edited after downloading and testing the plugin)

share|improve this answer
    
Hey Michael, it's throwing a "Invalid argument supplied for foreach()" error. –  Chad Sep 9 '11 at 14:30
    
possibly my mistake, as I don't know for sure what 'MRP_get_related_posts()' returns - you can test that with var_dump(MRP_get_related_posts( $post_id )) - or possibly because there are no related posts at the moment (reply corrected for that case). –  Michael Sep 9 '11 at 15:19
    
I tried the var_dump and I got 'null' - there is a explanation on this page about what the MRP_get_related_posts function can do. It's under API Functions - microkid.net/wordpress/related-posts/#API –  Chad Sep 9 '11 at 16:25
    
what is the value of $post_id ? if this is not defined, try to use $post->ID instead. –  Michael Sep 9 '11 at 16:57
    
answer edited after testing the plugin. if it works with $post->ID might depend on the location where you use the code. –  Michael Sep 9 '11 at 18:08

Rather than using get_posts, I'd recommend using get_the_title and get_the_post_thumbnail to get the data that you need. You can use them like:

echo get_the_title($post_id);
echo get_the_post_thumbnail($post_id, 'thumbnail');
share|improve this answer
    
Hey thanks for the info, the code outputted the title and thumbnail of the post that I am currently viewing not the posts from MRP_get_related_posts. My query might be wrong though. –  Chad Sep 9 '11 at 6:08

this worked for me:

<?php
    global $post;
    $post_type = ( $instance['post_type'] == 'all' ) ? null : $instance['post_type'];
    $related_posts = MRP_get_related_posts( $post->ID, 0, 0, $post_type );
    if( $related_posts ) {
        echo "<div id='boxes'><div class='container'> \n";
        foreach( $related_posts as $related_post_id => $related_post_title  ) {
            if ( '' != get_the_post_thumbnail($related_post_id) ) {
                $thumb_id = get_post_thumbnail_id($related_post_id);
                $thumb_url = wp_get_attachment_image_src($thumb_id, true);
                $theImage = $thumb_url[0];
            } else {
                $theImage = get_bloginfo('stylesheet_directory').'/img/trama-01.gif';
            }
            echo '<div class="box simple" style="background-image:url('.$theImage.')"><div class="box-content"><div class="wrapper"><div class="cover">';
            echo "<div class='titulo'><a href=\"".get_permalink( $related_post_id )."\"><h3>".get_post_meta($post->ID,'wpcf-tagline',TRUE)."</h3><h1>".$related_post_title."</h1></a></div>";
            echo "</div></div></div></div>\n";
        }
        echo "</div></div>";
    }
?>

The markup I needed to achieve was:

<div class='box simple' style="background-image:url(../img/banner-01.jpg)">
    <div class='box-content'>
        <div class='wrapper'>
        <div class='cover'>
            <div class="titulo">
                        <a href="#">
                            <h3>WP Types Tagline custom field here</h3>
                            <h1>Title here</h1>
                        </a>
                    </div>                    
                </div>
             </div>
        </div>
</div>

Hope it helps someone,

Best.

share|improve this answer
    
Don't just post code and says it worked. Always add an explanation to why it worked or to why your code should work –  Pieter Goosen Apr 19 at 5:48
1  
Ok, it worked because I had that particular markup -that I shared-, and I managed to get the display code out of the 'microkids-related-posts-widget.php' file and rearranged it to display recent posts in my single.php -where I needed them to appear- with the image, title and custom fields (I used WP Types plugin for custom fields BTW). –  fanta Apr 19 at 6:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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