2017-06-01 64 views

回答

0

你可以试试这个插件https://wordpress.org/plugins/category-posts/

如果你喜欢自己的代码。这里是:
您可以使用方法get_posts获取接受单个数组参数或字符串参数的结果,如下所示。

<?php $posts = get_posts('post_type=post&category=4'); 
$count = count($posts); 
echo $count; 
?> 

解释为上述代码: 它获取文章表wp_postspost_typepost并且属于category ID 4导致。然后我们使用php函数count获取总记录。

如果您只需要发布的帖子另外添加post_status=publish

function wpb_postsbycategory() { 
// the query 
$the_query = new WP_Query(array('category_name' => 'announcements', 'posts_per_page' => 10)); 

$posts = get_posts('post_type=post&category_name=announcements'); 
$count = count($posts); 

// The Loop 
if ($the_query->have_posts()) { 
    $string .= "<p>Total:<strong>{$count}</strong></p>"; 
    $string .= '<ul class="postsbycategory widget_recent_entries">'; 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
      if (has_post_thumbnail()) { 
      $string .= '<li>'; 
      $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array(50, 50)) . get_the_title() .'</a></li>'; 
      } else { 
      // if no featured image is found 
      $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>'; 
      } 
      } 
    } else { 
    // no posts found 
} 
$string .= '</ul>'; 

return $string; 

/* Restore original Post Data */ 
wp_reset_postdata(); 
} 
// Add a shortcode 
add_shortcode('categoryposts', 'wpb_postsbycategory'); 

// Enable shortcodes in text widgets 
add_filter('widget_text', 'do_shortcode'); 

通过使用上述功能,可以产生所得到的输出。 (仅供参考Show recent posts by category

只需访问Appearance » Widgets菜单,然后添加一个text widgetsidebar,接着在文本窗口小部件添加[categoryposts]短代码,并保存它。

+0

喜@gvgvgvijayan'<? php $ posts = get_posts('post_type = post&category = 4'); $ count = count($ posts); echo $ count; ?>'如何显示当前类别中的帖子数量,当前类别ID ,而不是特定的类别ID? –

+0

@ArianeMartinsGomesDoRego检查此https://wordpress.stackexchange.com/questions/247859/how-to-get-category-id-当前帖子 – gvgvgvijayan

+0

感谢朋友的回复:-)我昨天发现了一些东西:'<?php $ category = get_the_category(); echo'('。$ category [0] - > category_count。'posts)'; ?>'谢谢你:-) –

相关问题