【问题标题】:Display child categories when click parent category in wordpress在wordpress中单击父类别时显示子类别
【发布时间】:2016-08-30 06:05:27
【问题描述】:

我正在开发 WordPress 自定义主题并坚持实现以下功能。我什至不知道这是否可能!

我想在单击父类别时显示子类别,然后单击子类别将为您带来其中的帖子。在archive.php 中做会合适还是自定义模板?

非常感谢您的帮助和建议:)

【问题讨论】:

标签: php wordpress wordpress-theming


【解决方案1】:

自己找到了一个解决方案并分享它,以便帮助有需要的人:

好吧,我已经使用archive.php 完成了它,并且我使用了get_queried_object() 来获取当前查询的对象,该对象通过执行以下操作为我提供了一个对象:

$obj = get_queried_object();

print_r($obj);

它会给我们以下对象:

WP_Term Object
(
    [term_id] => 24
    [name] => BRIDAL
    [slug] => bridal
    [term_group] => 0
    [term_taxonomy_id] => 24
    [taxonomy] => category
    [description] => 
    [parent] => 0
    [count] => 0
    [filter] => raw
    [cat_ID] => 24
    [category_count] => 0
    [category_description] => 
    [cat_name] => BRIDAL
    [category_nicename] => bridal
    [category_parent] => 0
)

可以看到上面的对象中有一个[parent] => 0。所以就我而言,我是这样做的:

$obj = get_queried_object();

if ($obj->parent == 0) {
    // Display child categories on this cat
} else {
    // Display posts of the child category
}

希望对某人有所帮助?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 2013-09-03
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    相关资源
    最近更新 更多