如何在WordPress中的单个类别中显示全文?
你可以试试这个插件https://wordpress.org/plugins/category-posts/
如果你喜欢自己的代码。这里是:
您可以使用方法get_posts获取接受单个数组参数或字符串参数的结果,如下所示。
<?php $posts = get_posts('post_type=post&category=4');
$count = count($posts);
echo $count;
?>
解释为上述代码: 它获取文章表wp_posts
其post_type
是post
并且属于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 widget
您sidebar
,接着在文本窗口小部件添加[categoryposts]
短代码,并保存它。
喜@gvgvgvijayan''如何显示当前类别中的帖子数量,当前类别ID ,而不是特定的类别ID? –
@ArianeMartinsGomesDoRego检查此https://wordpress.stackexchange.com/questions/247859/how-to-get-category-id-当前帖子 – gvgvgvijayan
感谢朋友的回复:-)我昨天发现了一些东西:' category_count。'posts)'; ?>'谢谢你:-) –
哪里代码你的问题? – mickmackusa