0

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

  } 

?>

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()

  • Thank you! I can't make it work yet. I'm very beginner in php... I'd like to display it on a portfolio single page. Could you write me an easy stepbystep what should I do? – Peter Jan 14 at 15:27
  • I have added the new answer. Please past on the single portfolio page and check the response. – Mohsin Ghouri Jan 16 at 5:04
  • now it displays just one (the first one) from the ticked portfolio category list, but if the first on is not from the "6" parent, then it displays nothing: I explain in a new answer – Peter Jan 16 at 17:35
0
$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>';

      }  
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.