admin管理员组文章数量:1122832
I'd like to ask some help for this code for my wordpress page. I would like to display just the current portfolio categories on a portfolio page. I used this code, but it shows all the portfolio categories from the '6' parent. Thank you!
<?php
$args = array(
'hierarchical' => 1,
'taxonomy' => 'portfolio_category',
'hide_empty' => 0,
'parent' => 6,
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a><br>';
}
?>
I'd like to ask some help for this code for my wordpress page. I would like to display just the current portfolio categories on a portfolio page. I used this code, but it shows all the portfolio categories from the '6' parent. Thank you!
<?php
$args = array(
'hierarchical' => 1,
'taxonomy' => 'portfolio_category',
'hide_empty' => 0,
'parent' => 6,
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a><br>';
}
?>
Share Improve this question edited Jan 13, 2020 at 20:41 Peter asked Jan 13, 2020 at 20:15 PeterPeter 11 bronze badge3 Answers
Reset to default 0$term_id = $current_term_id;
$args = array(
'hierarchical' => 1,
'taxonomy' => 'portfolio_category',
'hide_empty' => 0,
'parent' => 6,
);
$categories = get_categories($args);
foreach($categories as $category) {
if( $term_id != $category->cat_ID){
continue;
}
echo '<a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a><br>';
}
I am assuming that you have current portfolio category in variable $current_term_id. However, if you don't have current category id then and you are in the archive page then you can get it by using get_queried_object()
$current_term = get_the_terms( get_the_ID(), 'portfolio_category' );
$term_id = '';
if( !empty( $current_term ) ){
$term_id = $current_term[0]->term_id;
}
$args = array(
'hierarchical' => 1,
'taxonomy' => 'portfolio_category',
'hide_empty' => 0,
'parent' => 6,
);
$categories = get_categories($args);
foreach($categories as $category) {
if( $term_id != $category->cat_ID){
continue;
}
echo '<a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a><br>';
}
now it displays just one (the first one) from the ticked portfolio category list, but if the first one is not from the "6" parent, then it displays nothing:
If I thick those:
parent 1
- --child 11
- --child 12
- --child 13
parent 2 (ID=6)
- --child 21
- --child 22 (X)
parent 3...
...
then display child 22 (it's the expected).
But if I tick those:
parent 1
- --child 11
- --child 12 (X)
- --child 13
parent 2 (ID=6)
- --child 21
- --child 22 (X)
parent 3...
...
then diplay nothing. In this case if I remove 'parent' => 6,
then it displays child 12.
本文标签: display current portfolio categories from a specific parent
版权声明:本文标题:display current portfolio categories from a specific parent 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736294642a1929401.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论