duanleiming2014 2014-09-30 00:38
浏览 36
已采纳

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

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; ?>

展开全部

  • 写回答

1条回答 默认 最新

  • doumingo04696 2014-09-30 00:44
    关注

    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') ) : ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部