如何在wordpress中获得帖子的特定特色图像。 ?

时间:2016-04-18 19:06:39

标签: php wordpress image post featured

在下面的代码中,我试图提取post_id = 25的精确特征图像,但相反,它正在拉出不同帖子的新图像。

                            <?php 
                            $post_ID= 25;
                            $post_url=  get_permalink($post_ID);
                            $queried_post = get_post($post_ID);
                            ?>
                            <img class="img-circle" src="<?php
                            if (has_post_thumbnail($post_ID)) {
                                the_post_thumbnail('medium');
                            }
                            ?>"
                                 <h2><a href="<?php get_permalink($post_url) ?>"</a><?php echo $queried_post->post_title; ?></h2>

                            <p><?php
                                query_posts('p=25');
                                if (have_posts()) : while (have_posts()) : the_post();
                                        ?>
                                    <div class="entry">
                                        <?php echo substr(get_the_excerpt(), 0, 300); ?><span>[...]</span>
                                    </div>
                                    <?php
                                endwhile;
                            endif;
                            ?>
                            <p><a class="btn btn-default"  href="#" role="button" >View details &raquo;</a></p>

1 个答案:

答案 0 :(得分:0)

您需要使用get_the_post_thumbnail才能传入ID。

if (has_post_thumbnail($post_ID)) {
   echo get_the_post_thumbnail($post_ID,'medium');
}

the_post_thumbnail实际上是这样做的:

function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
    echo get_the_post_thumbnail( null, $size, $attr );
}
如果传入null,

get_the_post_thumbnail将使用当前的帖子ID。

修改

您只需要更改此内容:

<?php 
$post_ID= 25;
$post_url=  get_permalink($post_ID);
$queried_post = get_post($post_ID);
?>
<img class="img-circle" src="<?php
if (has_post_thumbnail($post_ID)) {
    the_post_thumbnail('medium');
}
?>"

对此:

<?php 
$post_ID= 25;
$post_url=  get_permalink($post_ID);
$queried_post = get_post($post_ID);
?>
<img class="img-circle" src="<?php
if (has_post_thumbnail($post_ID)) {
   echo get_the_post_thumbnail($post_ID,'medium');
}
?>"