使用get_post_thumbnail在Wordpress上显示缩略图

时间:2015-09-19 09:34:29

标签: php wordpress

我很难在Wordpress页面上显示一些帖子缩略图。我使用高级自定义字段来选择这些特色文章。特色文章有特色图片。我可以正确显示标题和文章的链接,但我正在努力使用特色图片。

<?php 
    if (is_page('reviews')) {
    $featured_articles = get_field('featured_articles');

    $id = $featured_articles[0];
    echo "<div class='row blog-first'><div class='small-12 columns'><div class='row blog-teaser'>";
        echo "<div class='small-8 columns'>";
            if ( has_post_thumbnail($id) ) {
                echo "<a href='".get_permalink($id)."' class='thumb'>";
                    echo get_the_post_thumbnail( $id, 'teaser-thumbnail' ); 
                echo "</a>";
            }
        echo "</div>";
        echo "<div class='small-4 columns col-text'>";
            echo "<div class='d'></div>";
            echo "<a href='".get_permalink($id)."'><h3>".get_the_title($id)."</h3></a>";
            echo get_field('text_excerpt',$id);
        echo "</div>";
    echo "</div></div></div>";

    echo "<div class='row'>";
        echo "<div class='small-12 medium-8 columns'>";
            echo "<h2 class='h2 hc f2'> Our best reviews</h2>";
            for ($i=1; $i < 4; $i++) { 

                $id = $featured_articles[$i];
                echo "<div><div class='row blog-teaser'>";
                    echo "<div class='small-6 columns'>";
                        if ( has_post_thumbnail($id) ) {
                            echo "<a href='".get_permalink($id)."' class='image'>";
                                echo get_the_post_thumbnail( $id, 'teaser-thumbnail' );
                                $image_large = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'teaser-thumbnail');
                            echo "</a>";
                        }
                    echo "</div>";
                    echo "<div class='small-6 columns col-text'>";
                        echo "<div class='d'></div>";
                        echo "<a href='".get_permalink($id)."'><h3>".get_the_title($id)."</h3></a>";
                    echo "</div>";
                echo "</div></div>";

            }
        echo "</div>";

1 个答案:

答案 0 :(得分:0)

试试这个:

$featured_articles=get_field('featured_articles');
foreach($featured_articles as $single_feature){
    $img_arr=wp_get_attachment_image_src($single_feature,'medium');     //thumbnail,full,medium
    if(!empty($img_arr)){
        echo '<img src="'.$img_arr[0].'" alt="image">';     
    }else{
        echo '<img src="" alt="NO image">';     
    }
}