最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to extract images of post and pages excluding header and logo image in wordpress?

programmeradmin8浏览0评论

I have tried this code to get all images from media library and i am sucessfully getting the source urls of all the images but now i want to exclude all the unwanted images like logo, header images ,etc ...

In short i want to extract all images attached to posts and pages ..

if(is_single() || is_page() || is_home() ){  
          global $post;  

$query_images_args = array(
     'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,'numberposts' => 1
 );

 $query_images = new WP_Query( $query_images_args );
 $images = array();
         foreach ( $query_images->posts as $image) {
         $images[]= wp_get_attachment_url( $image->ID );

         }
            echo "<pre>";
             print_r($images);
             echo "</pre>"; 

My output Here the first image is a header image which is unwanted for me ..How to exclude it ..I have tried using attachment size but it cant be unique all the time .. Have a look at it

Array
(
    [0] => http://localhost/wordpress/wp-content/uploads/2013/03/AboutUsSlider.jpg
    [1] => http://localhost/wordpress/wp-content/uploads/2013/03/7325996116_9995f40082_n.jpg
    [2] => http://localhost/wordpress/wp-content/uploads/2013/03/6310273151_31b2d7bebe.jpg
    [3] => http://localhost/wordpress/wp-content/uploads/2013/03/4764924205_ce7470f15a.jpg
    [4] => http://localhost/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n.jpg
    [5] => http://localhost/wordpress/wp-content/uploads/2013/03/1494822863_aca097ada7.jpg
    [6] => http://localhost/wordpress/wp-content/uploads/2013/03/1385429771_453bc19702.jpg
)

I have tried this code to get all images from media library and i am sucessfully getting the source urls of all the images but now i want to exclude all the unwanted images like logo, header images ,etc ...

In short i want to extract all images attached to posts and pages ..

if(is_single() || is_page() || is_home() ){  
          global $post;  

$query_images_args = array(
     'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,'numberposts' => 1
 );

 $query_images = new WP_Query( $query_images_args );
 $images = array();
         foreach ( $query_images->posts as $image) {
         $images[]= wp_get_attachment_url( $image->ID );

         }
            echo "<pre>";
             print_r($images);
             echo "</pre>"; 

My output Here the first image is a header image which is unwanted for me ..How to exclude it ..I have tried using attachment size but it cant be unique all the time .. Have a look at it

Array
(
    [0] => http://localhost/wordpress/wp-content/uploads/2013/03/AboutUsSlider.jpg
    [1] => http://localhost/wordpress/wp-content/uploads/2013/03/7325996116_9995f40082_n.jpg
    [2] => http://localhost/wordpress/wp-content/uploads/2013/03/6310273151_31b2d7bebe.jpg
    [3] => http://localhost/wordpress/wp-content/uploads/2013/03/4764924205_ce7470f15a.jpg
    [4] => http://localhost/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n.jpg
    [5] => http://localhost/wordpress/wp-content/uploads/2013/03/1494822863_aca097ada7.jpg
    [6] => http://localhost/wordpress/wp-content/uploads/2013/03/1385429771_453bc19702.jpg
)
Share Improve this question asked Mar 29, 2013 at 9:14 user1145009user1145009 1231 gold badge1 silver badge6 bronze badges 4
  • This is from the related questions section on the right. Maybe it helps you determine which images are actually attached to a post, and which images are uploaded through Media Library but are "floating" there unattached. – Marc Dingena Commented Mar 29, 2013 at 9:22
  • Thanks @MarcDingena But it doesnt really meet my requirement..Bcoz i also want the images which are inserted in post content and page content... – user1145009 Commented Mar 29, 2013 at 9:57
  • Images inserted into post content by the media tools should be attached to the posts. There may be no generic solution for what you are asking. Without knowing how your images are uploaded and attached, or not attached, it may not be possible to construct the criteria you need to eliminate these images. In fact, there may not be clear criteria that you can put into code-- other than hard code the exceptions. I think you need to add more detail concerning these points. – s_ha_dum Commented Mar 29, 2013 at 13:29
  • @s_ha_dum I agree with your logically it is impossible to differentiate between images on basis of our criteria like logo or post content.. Can we just grab images of only post contents or page contents ..This is my imagination like we can just grab all images from all page templates with img tags while going through the contents of it..which might remove those unwanted images ...Or another way round is differentiate the images with its size ..second method do have some exceptions ..Anyways i have changed my logic ..Thanks for your knowledge.. – user1145009 Commented Mar 30, 2013 at 6:35
Add a comment  | 

1 Answer 1

Reset to default 0

I believe this will get you all of your attachments:

$q = new WP_Query(
  array(
    'post_type'=>'attachment',
    'post_status'=>'inherit'
  )
);
var_dump($q);

You could further restrict that by adding other parameters, for example, 'post_mime_type' => 'image' would restrict only to images.

That will return all images though, not just the ones attached to posts. What you want is post_parent != 0 but I don't see a way to do that with WP_Query. If you var_dump that query though you can steal its SQL and add the parameter, or build a posts_where filter.

发布评论

评论列表(0)

  1. 暂无评论