【问题标题】:How to get image url of a post in wordpress?如何在wordpress中获取帖子的图片网址?
【发布时间】:2013-04-25 09:08:30
【问题描述】:

我正在为 Wordpress 开发一个 Pinterest pinit 插件。 Pinterest pinit 按钮需要图像 URL 和发布 URL...我如何获取图像 URL?

我使用了下面的代码,但没有成功:

$pid=get_the_ID();
$image_id = get_post_thumbnail_id($pid);
$image_url = wp_get_attachment_image_src($image_id);
echo $image_url[0];

有人可以帮忙吗?

【问题讨论】:

  • 目前您正在获取帖子中特色图片的 ID。您是否正在尝试获取特色图片?

标签: php wordpress pinterest


【解决方案1】:

你也试过了吗?

$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
<img src="<?php echo $url; ?>" />

【讨论】:

    【解决方案2】:

    你也可以像这样使用 $catposts 是一个帖子特色图片查询。

    $category_id = 'your_category_id'; $NUMBEROFPOSTS = '4'; $catposts = get_posts('category='.$category_id."&order=DESC&numberposts=".$NUMBEROFPOSTS); 
    
    foreach ($catposts as $item) :
    
         echo '<li class="active" style="height: 100px; width: 71px;">';
    
         echo '<a href="'.get_permalink( $item->ID ).'">';
    
         echo '<img src="'.wp_get_attachment_url( get_post_thumbnail_id($item->ID)).'">';
    
         echo '</a>';
    
         echo '</li>';
    
    endforeach;
    

    【讨论】:

      猜你喜欢
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2012-06-30
      相关资源
      最近更新 更多