2013-04-17 51 views
0

我想获得我的类别3的所有图像附件ID。 有人知道该怎么做吗?从类别中获取所有图像url - Wordpress

这里是我的代码:

$query_images_args = array(
     'post_type' => 'attachment', 
     'post_mime_type' =>'image', 
     'post_status' => 'inherit', 
     'posts_per_page' => -1,  
//  'cat'=> 3, NOT WORKING 
     'orderby' => 'rand', // Order randomly 
    ); 

    $query_images = new WP_Query($query_images_args); 
    $images_desktop = array(); 
    $images_tablets = array(); 
    $images_smartphones = array(); 


    // WE ARE GETTING ALL IMAGES URLS ACCORDING TO THE DEVICE 
    foreach ($query_images->posts as $image) { 

    $attachment_width = wp_get_attachment_image_src($image->ID,'small'); 
    $attachment_width = $attachment_width[1]; 
     if($attachment_width<=500) 
     { 
      $images_smartphones[] = wp_get_attachment_url($image->ID); 
     } 
     elseif ($attachment_width<=1000) 
     { 
      $images_tablets[] = wp_get_attachment_url($image->ID); 
     } 
     elseif ($attachment_width>=1000){ 
      $images_desktop[]= wp_get_attachment_url($image->ID); 
     } 
    } 
    ?> 

我的想法:

获得第3类的所有帖子ID,如果他们有任何图像附件。 通过这个帖子ID列表,我可以获得每个附件ID的列表。 这是正确的吗?

感谢

+0

您需要首先获取在类别,然后将获得基于已检索HTTP职务图像的帖子://wordpress.stackexchange .COM /问题/ 56562 /如何对运行WP-查询 - 回收 - 附件到帖子,只从 - A-特定-美食 –

回答

1

这是我工作的代码

<?php wp_reset_query(); 

// Init 
    $images_desktop = array(); 
    $images_tablets = array(); 
    $images_smartphones = array(); 

    $args = array( 
     'orderby' => 'rand', 
     'post_type' => 'post', 
     'cat' => 3, 
     'posts_per_page' => -1, 
    ); 

    $wp_query = new WP_Query($args);     
// $wp_query->posts returns all posts even childrens 

    foreach ($wp_query->posts as $single_post) { 
     $single_post_ID = $single_post->ID; 
//  echo ($single_post_ID."<br/>"); 
     $args = array(
      'orderby' => 'rand', // Order randomly 
      'post_type'  => 'attachment', 
      'post_parent' => $single_post_ID, 
      'post_mime_type' => 'image', 
      'post_status' => null, 
      'numberposts' => -1, 
     ); 

     $attachments = get_posts($args); 
     if ($attachments) { 
      foreach ($attachments as $attachment) { 
       $attachment_ID = $attachment->ID; 

       $attachment_width = wp_get_attachment_metadata($attachment_ID); 
       $attachment_width = $attachment_width["width"]; 
       if($attachment_width<=500) 
       { 
        $images_smartphones[] = wp_get_attachment_url($attachment_ID); 
       } 
       elseif ($attachment_width<=1000) 
       { 
        $images_tablets[] = wp_get_attachment_url($attachment_ID); 
       } 
       elseif ($attachment_width>=1000){ 
        $images_desktop[]= wp_get_attachment_url($attachment_ID); 
       } 

      } 
     } 
    } 

    ?> 
相关问题