将所有图片附加到任何帖子

时间:2017-04-28 10:10:31

标签: php wordpress

我按以下方式获取所有图像的列表:

$the_query = new WP_Query( array(
  'post_type'       => 'attachment',
  'post_mime_type'  => 'image',
  'post_status'     => 'inherit',
  'posts_per_page'  => -1,
 ) );

但我只是想让那些上传到我网站上帖子的人。

不是附加到特定帖子的图片,而是排除未在任何帖子中展示的图片。

2 个答案:

答案 0 :(得分:1)

您所要做的就是将以下代码粘贴到循环中。

$args = array(
        'post_parent'    => get_the_ID(), // your post id
        'post_type'      => 'attachment',
        'numberposts'    => -1, // show all
        'post_status'    => 'any',
        'post_mime_type' => 'image',
        'orderby'        => 'menu_order',
        'order'           => 'ASC'
   );

$images = get_posts($args);
if($images) { ?>
    <img src="<?php echo wp_get_attachment_url($image->ID); ?>" />
<?php
}
?>

答案 1 :(得分:1)

我认为post_mime_type不正确。

$args = array(
    'post_type'  => 'attachment',
    'post_status'    => 'inherit',
    'post_mime_type' => 'image/gif',
);

$query = new WP_Query( $args );

希望这有帮助!

相关问题