Can we create a category list page in WordPress?

空扰寡人 提交于 2021-02-11 14:42:58

问题


I am working on a category. I have to show all the category list on-page. So I tried the below code and it's working. I am getting my all the category list.

Below is the output of my category list

My question is, Where I go and add the category list code? I have to access the URL like http://test.com/category/ so it will display my category list.

I created the category.php page but that page is displaying the post with the related category.

For example http://test.com/category/destination so it will display all the post which is related to the destination.

<?php
   /**
   * A Simple Category Template
   */
   get_header(); 

?>  
<div id="primary" class="content-area">
   <main id="main" class="site-main">
       <div class="CategoryHero">
<?php
// Get the current queried object
$term    = get_queried_object();
$term_id = ( isset( $term->term_id ) ) ? (int) $term->term_id : 0;

$categories = get_categories( array(
    'taxonomy'   => 'category',
    'orderby'    => 'name',
    'parent'     => 0,
    'hide_empty' => 0, // change to 1 to hide categores not having a single post
) );
?>

<ul>
    <?php
    foreach ( $categories as $category ) 
    {
        $cat_ID        = (int) $category->term_id;
        $category_name = $category->name;

        // When viewing a particular category, give it an [active] class
        $cat_class = ( $cat_ID == $term_id ) ? 'active' : 'not-active';

        // I don't like showing the [uncategoirzed] category
        if ( strtolower( $category_name ) != 'uncategorized' )
        {
            printf('<li><a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . $category->name . '</a></li>');
        }
    }
    ?>
</ul>
     
         </div>
   </main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Would you help me out with this issue?


回答1:


You can create a separate category(taxonomy) page on WordPress by taxonomy-{tax-name}.php and put this code in that file. Please let me know if it helps you to display the following data on a separate page. Regards



来源:https://stackoverflow.com/questions/63279899/can-we-create-a-category-list-page-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!