【问题标题】:WordPress - Show children of the visited category onlyWordPress - 仅显示访问类别的孩子
【发布时间】:2014-12-08 14:38:53
【问题描述】:

我使用wp_list_categories() 创建了一个菜单,但它显示的类别及其所有子项都可见。我的客户希望我让它只显示当前类别的孩子。

假设我们有一个类别树:

第一类

第二类

第三类

如果有人点击第一个类别,那么该类别的子项应该是可见的:

第一类

  • 孩子1
  • 孩子2
  • child3

第二类

第三类

只要他点击一个子类别,例如 child1,它应该如下所示:

第一类

  • 孩子1
    • child1 的 child1
    • child1 的child2
  • 孩子2
  • child3

第二类

第三类

最后,只要他点击,例如 child1 的 child2:

第一类

  • 孩子1
    • child1 的 child1
    • child1 的 child2
      • child1 of child2 of child1
  • 孩子2
  • child3

第二类

第三类

因此,例如,如果他处于 3 个类别的深度,那么同一级别上的所有其他类别都应该使其子项不可见。一旦他选择了其中一个子类别,它就应该显示它的子类别。

这让我大吃一惊,我不知道如何使用 jQuery/css 来做到这一点。 WordPress 类在这里似乎没用。你能帮我吗?如果可以使用 wp_list_categories() 函数的基本 WordPress 参数来完成,那就太好了:

$args = array(
'show_option_all'    => '',
'orderby'            => 'name',
'order'              => 'ASC',
'style'              => 'list',
'show_count'         => 0,
'hide_empty'         => 0,
'use_desc_for_title' => 1,
'child_of'           => 0,
'feed'               => '',
'feed_type'          => '',
'feed_image'         => '',
'exclude'            => '',
'exclude_tree'       => '',
'include'            => '',
'hierarchical'       => 1,
'title_li'           => __( '' ),
'show_option_none'   => __( 'No categories' ),
'number'             => null,
'echo'               => 1,
'depth'              => 0,
'current_category'   => 0,
'pad_counts'         => 0,
'taxonomy'           => 'category',
'walker'             => null
);
wp_list_categories( $args ); 

【问题讨论】:

    标签: php jquery wordpress


    【解决方案1】:
    li.categories .children
    {
      display: none;
    }
    li.categories .current-cat > .children,
    li.categories li.current-cat-ancestor > .children
    {
      display: list-item;
    }
    

    这应该足够了。 :)

    在第 3 级之后,由于 WordPress 错误,您可能还需要 jQuery...

    jQuery(function($) {
      $('.current-cat').parents('.cat-item').addClass('current-cat-ancestor');
    });
    

    【讨论】:

    • 嗯,它只能工作到第 3 级,4 级的猫打破一切......我认为这是 WordPress 错误。
    • 我创建了一个核心票务女巫补丁来修复它(没有 jQuery 解决方法) - 如果你给它加星标,也许我们可以在 4.1 中看到它。 :) core.trac.wordpress.org/ticket/30635
    猜你喜欢
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多