【问题标题】:How to create taxonomy archive that can display list of post of that taxonomy如何创建可以显示该分类的帖子列表的分类档案
【发布时间】:2014-10-01 03:01:13
【问题描述】:

我正在创建可以使用自定义分类法(如类别)的 wordpress 模板。我希望每次单击帖子的类别时都会显示该类别的帖子列表。这是我的 category.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 中更改什么代码才能满足显示自定义分类档案的需求(可以显示与点击的分类相关的所有帖子)?

【问题讨论】:

    标签: php wordpress custom-taxonomy


    【解决方案1】:

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

    编辑

    在注册您的两个分类时,您有一个重大的根本缺陷。

    不能在自定义分类名称中包含空格,这也适用于函数名称和自定义帖子类型名称。以下规则适用,没有可以解决这些问题。

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

    • 只允许小写字母

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

    • 有关分类法和帖子类型的字符数限制,请参阅法典

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

    【讨论】:

    • 您在注册分类时是否将public 设置为true?另外,您是否刷新了永久链接?
    • 是的,我已将公共属性设置为 true,我也刷新了永久链接,但没有任何改变。这是我注册分类的代码:jsfiddle.net/bkrs2bvk
    【解决方案2】:

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

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

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

    【讨论】:

    • +1。你的回答比我的更全面。今天早上离开家前我在飞行中做的:-)
    • 它仍然无法正常工作。它显示找不到页面。我的分类名称是 Kategori Berita,所以我创建了 taxonomy-Kategori Berita.php 作为自定义分类模板。
    • @IPutuSaputra - 你介意分享创建分类的代码吗?目前,您正在使用 taxonomy-.php 文件中的分类标签。这不应该是名称,而是分类的标识符(在您的情况下可能类似于 kategori-berita - 但这取决于您如何注册分类。如果您分享您的代码,那么我会让您知道什么您的分类标准-.php 文件的名称应该是。
    猜你喜欢
    • 2015-05-10
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多