我们可以在WordPress中创建类别列表页面吗? [英] Can we create a category list page in WordPress?
问题描述
我正在研究一个类别.我必须在页面上显示所有类别列表.所以我尝试了下面的代码,它正在工作.我正在获取所有类别列表.
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.
以下是我的类别列表的输出
我的问题是,我要去哪里添加类别列表代码?我必须访问 http://test.com/category/
之类的URL,这样它才能显示我的类别列表.
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.
我创建了 category.php
页面,但是该页面显示的是带有相关类别的帖子.
I created the category.php
page but that page is displaying the post with the related category.
例如 http://test.com/category/destination
,这样它将显示与目的地有关的所有帖子.
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?
推荐答案
您可以通过taxonomy- {tax-name} .php在WordPress上创建一个单独的category(taxonomy)页面,并将此代码放入该文件中.请让我知道它是否有助于您在单独的页面上显示以下数据.问候
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
这篇关于我们可以在WordPress中创建类别列表页面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!