如何创建可以显示该分类的帖子列表的分类归档


How to create taxonomy archive that can display list of post of that taxonomy

我正在创建可以使用自定义分类法(如类别)的wordpress模板。我希望每次我点击帖子的类别时,它都会显示该类别的帖子列表。这是我的分类.php.

<?php get_header(); ?>
<body>
<div id="container">
    <div id="header"><h1><a href="<?php echo get_option('home'); ?>"><?php bloginfo('name'); ?></a></h1></div>
    <div id="menu"><?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'menu_class' => 'nav', 'theme_location' => 'primary-menu' ) ); ?></div>
    <div id="content">
        <div id="blog">
        <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
            <?php /* If this is a category archive */ if (is_category()) { ?>
                <h2>Archive for the &#8216;<?php single_cat_title(); ?>&#8217; Category:</h2>
            <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
                <h2>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h2>
            <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
                <h2>Archive for <?php the_time('F jS, Y'); ?>:</h2>
            <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
                <h2>Archive for <?php the_time('F, Y'); ?>:</h2>
            <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
                <h2>Archive for <?php the_time('Y'); ?>:</h2>
            <?php /* If this is an author archive */ } elseif (is_author()) { ?>
                <h2>Author Archive</h2>
            <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
                <h2>Blog Archives</h2>
        <?php } ?>
        <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
        <div class="post">
        <br>
        <br>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="entry">
            <br>    
                <?php the_content(); ?>
                <p class="postmetadata">
                <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
                <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
                </p>
            </div>
            <br>
            <hr>
            <br>
        </div>
        <?php endwhile; ?>
        <div class="navigation">
        <?php posts_nav_link(); ?>
        </div>
        <?php endif; ?>
    </div>
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

我已经找了一整天了,什么都没找到。我应该使用什么自定义分类模板归档名称?我必须在这个category.php中更改哪些代码才能满足显示自定义分类归档(可以显示与单击的分类相关的所有帖子)的需要?

您应该为属于自定义分类法的术语创建一个taxonomy.php。您可以简单地复制上面的模板,并将其称为taxonomy.php

编辑

在注册两个分类法时,您有一个很大的基本缺陷。

自定义分类法名称中不能有空格,这也适用于函数名称和自定义post-type名称。以下规则适用,并且没有解决这些问题的方法。

  • 名称不能包含空格或任何特殊字符。

  • 只允许使用小写字母

  • 如果名称包含多个单词,则只能使用下划线(_)来分隔它们。不要使用连字符(-),因为您稍后在上会遇到问题

  • 有关分类法和后类型的字符数限制,请参阅代码集

因此,简而言之,kategori beritatag berita是无效名称。它们应该是kategori_beritatag_berita。正如我所说,有没有解决这个问题的方法。您唯一的解决方案是相应地更改您的分类名称

当您的分类法的名称为"roles"时,您可以创建一个名为taxonomy-roles.php的文件。您也可以用类似这样的术语(在本例中为"ceo")来扩展它:taxonomy-roles-co.php。

正如@pietergoosen所说,你可以复制粘贴你在这些文件中提供的代码。

有关更多信息,您可以查看以下链接:http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display