是否有允许页面显示来自某个特定类别的所有帖子的 wordpress 功能?

Is there a wordpress function that allows a page to display all posts from some specific category?

我们有一个 wordpress 功能来显示所有帖子,例如-

<?php
if ( have_posts() ) {
    while ( have_posts() ) {

        the_post(); ?>

        <h2><?php the_title(); ?></h2>

        <?php the_content(); ?>

    <?php }
}
?>

wordpress 中是否有任何功能可以只显示来自某个特定类别的帖子?

喜欢 -

<?php
if ( have_posts() ) {
    while ( have_posts() ) {

        the_post(); ?>

        <h2><?php the_title(); ?></h2> //from Category = X

    <?php }
}
?>

您可以简单地使用函数获取它:

<?php query_posts( 'cat=x' ); ?>

其中 "x" 是您的类别 ID

此功能提供了多种优势,因为您可以过滤和排序结果


获取 2004 年的类别 3 帖子

<?php query_posts( 'cat=3&year=2004' ); ?>

获取 2004 年的第 1 类帖子并按日期升序排列。

<?php query_posts( 'cat=1'.'&year=2004&orderby=date&order=asc'); ?>

想要检索实际类别?只需使用:

<?php 

$catId = get_cat_ID('MYCATEGORY'); //get current category id

query_posts( 'cat='.$catId ); // query the posts

if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<h1>
    <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
    </a>
</h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>