WordPress:如何只显示某个类别的帖子?

Jan*_*nis 3 php wordpress list filter categories

我是WordPress的新手,但花了大约50个小时的时间来研究它,尝试一下这样的事情,感觉我现在已经有了很好的处理.

然而,我根本无法工作的一件事是让一个页面吐出某个类别的帖子列表.

这是我的例子:http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters

我有一个帖子,如果我直接使用它可以正常工作,但不会显示在此页面上.

帖子直接链接.

类别ID为"3",而类别名称为"印刷海报".

我有一个印刷海报页面的自定义页面模板,如下所示:

<?php
/*
Template Name: Typographic Posters
*/
?>

<?php get_header(); ?>
<?php get_sidebar(); ?>

<?php if (in_category('3')): ?>
<div class="post">

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


  <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
   <div class="post-description">
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
   </div>
   <?=get_image('flutter-image');?>
  </div>


    <?php endwhile; else: ?>
     <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

</div>
<?php endif; ?>

<?php get_footer(); ?>
Run Code Online (Sandbox Code Playgroud)

然而,使用此代码只显示页面获取标题,侧边栏和其他内容.

如果有人可以帮助我,那将真正帮助我处理wordpress类别的过滤.

谢谢阅读,

Jannis

小智 11

in_category只能在单个页面上的循环外工作.我建议使用该query_posts函数来解决这个问题.您可以使用query_posts('cat=3')query_posts('category_name=typographic-posters')获取您要查找的帖子.

获得后,只需使用普通的WordPress循环即可访问这些帖子.