简体   繁体   English

如何在子类别页面中显示Wordpress类别?

[英]How to display Wordpress Categories in Sub-Categories Page?

Ok so let me start off by telling you my goal then you can look at the example below which will make more sense. 好的,让我先告诉你我的目标,然后你可以看看下面的例子,这将更有意义。 I am trying to achieve a simple filter with categories so to speak. 我试图用类别来实现一个简单的过滤器。 I am creating a simple custom post type named "Cars" and it will have a field for an image and description of all cars. 我正在创建一个名为“Cars”的简单自定义帖子类型,它将包含一个用于所有汽车的图像和描述的字段。 Now I am displaying that custom post type through a loop and filtering them by assigning them to specific categories and displaying them in the category.php through conditionals. 现在我通过循环显示自定义帖子类型,并通过将它们分配给特定类别并通过条件在category.php中显示它们来过滤它们。

So I have a Category of HONDA and 3 Sub-Categories of that category: 所以我有一个HONDA类别和该类别的3个子类别:

  • Accord 符合
  • Civic 思域
  • Prelude 序幕

Now when I am on the "Cars" Page it will display ALL Categories custom post type and on the left hand side there will be a sidebar with all the categories of CARS such as HONDA - TOYOTA - NISSA 现在,当我在“汽车”页面上时,它将显示所有类别自定义帖子类型,在左侧将有一个侧栏,其中包含所有类别的CARS,例如HONDA - TOYOTA - NISSA

When I click on a category which is a car make in the left it will display all the custom post types that are assigned to that specific category and on the sidebar where the categories displayed now it will only display HONDA categories and it's Sub-Categories 当我点击左侧的汽车品牌类别时,它将显示分配给该特定类别的所有自定义帖子类型,并且在侧边栏上显示的类别现在它将仅显示HONDA类别及其子类别

NOW THE PROBLEM IS: When I click on a HONDA sub-category such as Accord the sidebar displaying all the car categories related to that sub-cat does not display any more when it should. 现在的问题是:当我点击像Accord这样的HONDA子类别时,显示与该子猫相关的所有汽车类别的侧栏在它应该的时候不再显示。

CLICK HERE FOR THE EXAMPLE (this should make everything a lot more clear) 点击这里 查看 示例 (这应该使一切更加清晰)

And here is the logic I am using to filter what categories display on the sidebar depending on what category it is in. 以下是我用来过滤侧边栏上显示的类别的逻辑,具体取决于它所在的类别。

<?php if (is_category('Honda') ) : ?>

    <?php $args = array(
    'orderby'            => 'name',
    'order'              => 'ASC',
    'style'              => 'list',
    'show_count'         => 1,
    'hide_empty'         => 1,
    'use_desc_for_title' => 1,
    'child_of'           => 5,
    'hierarchical'       => 1,
    'title_li'           => __( 'Categories' ),
    'show_option_none'   => __( 'No categories' ),
    'number'             => null,
    'echo'               => 1,
    'taxonomy'           => 'category',
    'walker'             => null
); ?>

<?php wp_list_categories( $args ); ?>

<?php else : ?>
<p>This is some generic text to describe all other category pages.</p>
<?php endif; ?>

There is also the Wordpress function in_category 还有Wordpress函数in_category

http://codex.wordpress.org/Function_Reference/in_category http://codex.wordpress.org/Function_Reference/in_category

So your code should look something like this: 所以你的代码应该是这样的:

<?php if (is_category('Honda') || in_category('Honda') ) : ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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