【问题标题】:Get first image of custom post type wordpress获取自定义帖子类型 wordpress 的第一张图片
【发布时间】:2017-11-01 23:50:50
【问题描述】:

我有这个代码

function display_categoria($args) {
$query = new WP_Query(array(
    'post_type' => 'job_listing',
    'post_status' => 'publish',
    'posts_per_page' => 5
));


while ($query->have_posts()) {
  echo $query->the_post();
   $id=get_the_id();
   echo $query1=get_permalink();
  }

 wp_reset_query();
}

add_shortcode( 'este', 'display_categoria' );

理论上我可以在循环中解决它

 if ( has_post_thumbnail() ) {
the_post_thumbnail();
                            } 

但是很多条目没有缩略图(特色图片),能看懂吗?

【问题讨论】:

  • 所以有些确实有缩略图?对于那些没有的人,您是否检查过他们的帖子中是否确实设置了特色图片?
  • 对不起,我的意思是推荐图片@TurtleTread

标签: php wordpress loops


【解决方案1】:

这应该检索每个帖子的第一张图片的网址。在“$id=get_the_id();”之后插入它线

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'posts_per_page' => -1,
    'post_status' => null,
    'post_parent' => $id 
); 
$images = get_posts( $args );
if ( $images ) {
    $first_image_id = $images[0];

    //do something with the image

    wp_reset_postdata();
}

【讨论】:

    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2016-10-28
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多