2013-03-29 1 views
0

나는 미디어 라이브러리에서 모든 이미지를 얻으려고이 코드를 시도했지만 모든 이미지의 소스 URL을 성공적으로 받고 있지만 로고, 헤더 이미지 등의 원치 않는 이미지는 모두 제외하고 싶습니다.Wordpress에서 머리글 및 로고 이미지를 제외한 게시물 및 페이지의 이미지를 추출하는 방법은 무엇입니까?

즉 내가

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>"; 

내 출력 여기에 첫 번째 이미지는 나를 ..How 위해 원치 않는 것입니다 헤더 이미지를 사용하여 시도했다 .. 난을 제외하는 것입니다 .. 게시물과 페이지에 연결된 모든 이미지를 추출 할 첨부 파일 크기가 있지만 항상 고유 할 수는 없습니다 .. 살펴보기

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 
) 

답변

0

이 시점부터 필터링 할 항목을 지정할 수 있습니다.

$images[]= wp_get_attachment_url($image->ID); 
} 
$linkstoremove[] = $linksyouwanttoremove; 
//remove unwanted images 
$filtered_images = array_filter($images, $linkstoremove); 
//set data sequence 
$filtered_images = array_values($filtered_images); 
print_r($filtered_images); 
0

쿼리 DB를하고는 필터를 적용하고 그 게시물 내에있는 각 게시물 및/또는 이미지 모든 이미지에서 추출, 게시물을 가져옵니다. 예 :

$my_images = array(); 
    $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); 
    while ($query_images->have_posts()) : $query_images->the_post(); 

     $dcontent = apply_filters('the_content', get_the_content()); 
     $dcontent = preg_replace("/\< *[img][^\>]*[.]*\>/i","",$dcontent,1); 
     if (preg_match_all('/<img (.+?)>/', $dcontent, $matches)) { 
        foreach ($matches[1] as $match) { 
         foreach (wp_kses_hair($match, array('http')) as $attr) 
          $img[$attr['name']] = $attr['value']; 
          $my_images[] = $img['src']; 
        } 
       } 
    endwhile; 

100 % 효율이 아니지만 작동합니다.

관련 문제