0

我想从 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;

  }

我想计算活动/已发布帖子中的总图像数。我无法在博客中获得已发布的帖子,也无法在单个帖子中获得图像..但是如何整合它以获得所有总数???

4

1 回答 1

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();
于 2013-05-27T12:44:00.220 回答