[英]How to extract images of post and pages excluding header and logo image in wordpress?
我已经尝试过此代码从媒体库中获取所有图像,并且我成功获取了所有图像的源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
)
从这一点上,您可能想要指定要过滤的内容。
$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);
查询数据库并获取帖子,然后应用任何过滤器并从每个帖子中提取和/或对这些帖子中的所有图像进行映像。 例如:
$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%,但可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.