简体   繁体   中英

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

I am creating wordpress template that can use custom taxonomy(like categories). i want that every time i click the post's category it shows the list of post of that category. here is my 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(); ?>

I already searching whole day and nothing working. what custom taxonomy template archive name i should use ? what code that i must change in this category.php to fulfill the needs of displaying the custom taxonomy archives(that can display all post that related to the clicked taxonomy) ?

You should create a taxonomy.php for terms belonging to a custom taxonomy. You can just simply copy the template above and call it taxonomy.php

EDIT

You have one big fundamental flaw when registering your two taxonomies.

You cannot have white spaces in your custom taxonomy names, and this applies to function names and custom post type names as well. The following rules apply, and there is no work around these.

  • names cannot contain white spaces or any special character.

  • only lowercase letters are allowed

  • if a name contains more that one word, you can only use underscores ( _ ) to separate them. Do not use hyphens ( - ) as you will have issues later on

  • see the codex for the limitation on character count for taxonomies and post types

So, in short, kategori berita and tag berita is invalid names. They should be kategori_berita and tag_berita . As I said, there are no work around to this problem. Your only solutions is to change your taxonomy names accordingly

When the name of your taxonomy is "roles", you could create a file called taxonomy-roles.php. You can expand this with the term (in this example "ceo") like this as well: taxonomy-roles-ceo.php.

As @pietergoosen said you can just copy paste the code that you provided with in those files.

For more info on this you can check out the following link: http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM