【问题标题】:Display Thumbnails on Wordpress with get_post_thumbnail使用 get_post_thumbnail 在 Wordpress 上显示缩略图
【发布时间】:2015-12-16 11:07:17
【问题描述】:

我很难在 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>";

【问题讨论】:

  • 您是否使用add_theme_support( 'post-thumbnails' ); 添加了对post-thumbnails 的主题支持并且teaser-thumbnail 是已注册的图像大小?当您尝试输出缩略图时会发生什么?
  • 显然我也没有这样做。所以我只添加 add_theme_support('post-thumbnails');到我的主题-function.php。如何定义图像大小?我只想输出一个中等大小的缩略图。
  • 在这种情况下,我认为您应该像这样调用函数get_the_post_thumbnail( $id, 'thumbnail' );。您可以在位于Settings &gt; MediaMediaPanel 下管理尺寸

标签: php wordpress


【解决方案1】:

试试这个:

$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">';     
    }
}

【讨论】:

  • 谢谢,它显示了 else 语句,尽管显然至少有一篇精选文章有图片。
  • 不知道为什么它不起作用。主题中的其他地方使用了“中拇指”。
  • 好的,我需要为我的 get 字段传递完整参数,如下所示:$featured_articles = get_field('featured_articles', false, false);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
相关资源
最近更新 更多