【问题标题】:How can i get first image if the post has no post thumbnail in wordpress如果帖子在 wordpress 中没有帖子缩略图,我如何获得第一张图片
【发布时间】:2014-09-09 07:07:54
【问题描述】:

我为我的主题中的热门、最近和评论最多的帖子创建了侧边栏小部件。我有一些不包含图像缩略图的帖子。 这是我的小部件中 5 个帖子的热门查询帖子

<?php if (!empty($popular_posts)) { ?>
            <div class="tab-pane fade in active" id="popular">
                <div class="row">

                <!-- ********************************************** -->
                <!-- Popular Posts Tab -->
                <!-- ********************************************** -->
                <?php
                    $YPE_options = get_option( 'YPE_sidebar_option_name' ); 
                    $popular = new WP_Query( array( 
                        'posts_per_page' => $popular_limit, 
                        'meta_key' => 'post_views_count', // this is function within functions.php for counting post veiews
                        'orderby' => 'meta_value_num', 
                        'order' => 'DESC'  
                    ));
                while ( $popular->have_posts() ) : $popular->the_post();
                    $html  = '<article>';
                    $html .= '<section class="bootstrap-nav-thumb">';
                    $html .= '<p>';
                    $html .= '<a href="' . get_permalink() . '">';
                    $html .=  get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive '.$YPE_options['YPE_sidebar_PRC_thumb_style'].''));
                    $html .= '</a>';
                    $html .= '</p>';
                    $html .= '</section>';

                    $html .= '<aside class="bootstrap-title-info">';
                    $html .= '<p>';
                    $html .= '<a href="' . get_permalink() . '">'.get_the_title().'</a>';
                    $html .= '</p>';
                    $html .= '<p class="text-muted">' . get_the_date() . '||'. getPostViews(get_the_ID()) . '</p>';
                    $html .= '</aside>';
                    $html .= '</article>';
                    echo $html;
                endwhile;   
                ?>
                    </div> <!-- End row of popular posts -->
                </div> <!-- End tab-pane of popular posts -->   
        <?php } ?>

如何在这段代码中添加条件语句

$html .= '<a href="' . get_permalink() . '">';
                    $html .=  get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive '.$YPE_options['YPE_sidebar_PRC_thumb_style'].''));
                    $html .= '</a>';

注意:我想说是否有帖子缩略图放置帖子缩略图,如果没有缩略图则放置第一张图片而不是帖子缩略图

【问题讨论】:

  • 请解释一下第一张图片。您是在谈论帖子内容中的图像吗?

标签: php wordpress


【解决方案1】:
$thumb = get_the_post_thumbnail(get_the_ID());
if(!empty($thumb))
$image = $thumb;
else {
$image = '<img src="';
$image .= catch_that_image();
$image .= '" alt="" />'; }

并把你的功能作为

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}

【讨论】:

  • 感谢使用您的代码解决了我的问题
【解决方案2】:

这样的事情可能会起作用:

$html .= '<a href="' . get_permalink() . '">';
if ( has_post_thumbnail() ) {
     $html .=  get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive '.$YPE_options['YPE_sidebar_PRC_thumb_style'].''));
} else {
     $html .= "<img src='". echo wp_get_attachment_image_src(0,'thumbnail') .'" class="img-responsive ' .$YPE_options['YPE_sidebar_PRC_thumb_style'].' " />';
};
html .= '</a>';

【讨论】:

    猜你喜欢
    • 2011-12-28
    • 2012-11-23
    • 2014-03-12
    • 2014-10-17
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 2016-01-31
    • 2016-02-28
    相关资源
    最近更新 更多