【问题标题】:How to get specific featured images of the post in wordpress. ?如何在 wordpress 中获取帖子的特定特色图片。 ?
【发布时间】:2016-08-10 16:25:01
【问题描述】:

在下面的代码中,我试图提取 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>

【问题讨论】:

    标签: php wordpress image post featured


    【解决方案1】:

    您需要使用 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 );
    }
    

    get_the_post_thumbnail 如果传入 null,则依次使用当前帖子 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');
    }
    ?>"
    

    【讨论】:

    • 感谢安德鲁的回复。我在 function.php 文件中添加函数并更新 if (has_post_thumbnail 代码....它不起作用。现在我的网站已关闭
    • 嗯,不,你不需要向functions.php添加任何内容,我将编辑答案以使其更清晰
    猜你喜欢
    • 2012-06-30
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多