0

メディア ライブラリからすべての画像を取得するためにこのコードを試してみましたが、すべての画像のソース URL を正常に取得していますが、ロゴ、ヘッダー画像などの不要な画像をすべて除外したいと考えています ...

要するに、投稿とページに添付されているすべての画像を抽出したい..

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

私の出力ここで最初の画像は、私にとって不要なヘッダー画像です..それを除外する方法..添付ファイルのサイズを使用してみましたが、常に一意にすることはできません..それを見てください

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
)
4

2 に答える 2

0

データベースにクエリを実行して投稿を取得し、フィルターを適用して、各投稿および/または画像からそれらの投稿内のすべての画像を抽出します。例えば:

    $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% 効率的ではありませんが、機能します。

于 2014-01-03T10:32:32.867 に答える
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);
于 2013-04-03T22:49:23.190 に答える