简体   繁体   中英

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. 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.

So I have a Category of HONDA and 3 Sub-Categories of that category:

  • 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

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

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.

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

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

So your code should look something like this:

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

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