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 badge
Add a comment  | 

3 Answers 3

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