doushi1996 2019-05-14 15:41
浏览 42

无法在Wordpress中获取特定的帖子附件

I am trying to retrieve all of the images associated with a post. I am able to return the ID and to print the ID but when I try to add it with the arguments for get_posts() it returns an empty array. If i don't include a post_parent it gives me all of my images that I have in my media library not all of the images for the post I am looping over. Am I not looping at the right level?

Ive tried manually adding the ID and it still returns an empty array.

$attachments = get_posts( array(
     'post_type'      => 'attachment',
     'posts_per_page' => 1,
     'post_parent'    => get_the_ID()
) );

var_dump($attachments);

I am expecting a list of the attachments for the post. I want to grab an image from the attachments and print it to the page. But I keep getting empty arrays. Thanks for any help!

  • 写回答

2条回答 默认 最新

  • doumeng9188 2019-05-14 16:24
    关注

    The Wordpress Codex says:

    Whilst the default WP_Query post_status is 'publish', attachments have a default post_status of 'inherit'. This means no attachments will be returned unless you also explicitly set post_status to 'inherit' or 'any'

    So you should try to add 'post_status' => 'inherit' to your arguments array.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化