我想从wordpress的博客页面获取图片数量。我有一个代码来获取图像nw但它只给我个人帖子的图片..
以下是我的代码,以便在帖子中获取图像..
$count_posts1 = wp_count_posts();
$published_posts = $count_posts1->publish;
echo "<br />number of post -----".$published_posts;
$postimg = array();
for ($j=0; $j<$published_posts; $j++)
{
$szPostContent = $post->post_content;
$szSearchPattern = '#(<img.*?>)#';
$a='';
$aPics='';
preg_match_all( $szSearchPattern, $szPostContent, $aPics );
$iNumberOfPics = count($aPics[$j]);
echo "<br />Number of pics on each post...............".$iNumberOfPics;
}
我想了解活跃/已发布帖子中的总图片数量。我可以在博客中找不到已发布的帖子,也没有单个帖子中的图片。但是如何整合它以获得所有总数?
答案 0 :(得分:1)
您可以尝试这样
query_posts('post_status=publish');
while (have_posts()) : the_post();
$id = get_the_ID();
$total_attachments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND post_parent=$id");
echo $total_attachments;
endwhile;
wp_reset_query();