Trouble using get_post

时间:2011-09-09 作者:Chad

适用于微型儿童related post plugin他说

“使用get\\u post()函数,您可以从相关帖子中获取所需的任何数据。”

他发布了这段代码。

$related_posts = MRP_get_related_posts( $post_id );
我不太确定如何在这种情况下使用get\\u post功能。我想显示该数组中每个帖子的标题和缩略图。任何帮助都将不胜感激。

3 个回复
最合适的回答,由SO网友:Michael 整理而成

可能:

$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);   
    }
(下载并测试插件后编辑)

SO网友:tollmanz

而不是使用get_posts, 我建议使用get_the_titleget_the_post_thumbnail 以获取所需的数据。您可以像这样使用它们:

echo get_the_title($post_id);
echo get_the_post_thumbnail($post_id, \'thumbnail\');

SO网友:fanta

这对我很有用:

<?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>";
    }
?>
我需要实现的标记是:

<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>
希望它能帮助别人,

最好的

结束

相关推荐