I need to do the following: The sense if I have a class dportfolio - show a category dportfolio. Otherwise show usual category.

I use:

if ( class_exists( 'DPortfolio' ) ) {
      echo DPortfolio::instance()->dportfolio_get_category();
}; ?>

and when I try to add a second part of a code:

the_category(', ')

with if/else statement, I receive a php error.

Any suggestions to write this function in a correct way?


Is the code used inside of the loop? The following code should work:

if ( class_exists( 'DPortfolio' ) ) {
      echo DPortfolio::instance()->dportfolio_get_category();
    //or echo get_the_category();

If outside of the loop you can try echo get_the_category( $post_id ) to show the categories.

Note: As you are getting an error, if you edit the question, include the full code snippet with if/else and include the error, then it's easier to help.

|improve this answer|||||
  • I use this code inside a loop. If I use your code, I receive this error: "Warning: Invalid argument supplied for foreach() in class-dportfolio.php on line 78". I receive this error in a posts loop and in a post single. – MichaelNagval Oct 21 '14 at 8:40
  • And on line 78, in class-dportfolio.php I have this function: public function dportfolio_get_category() { global $post; $terms = get_the_terms( $post->ID, 'dportfolio_categories' ); $content = ''; foreach ($terms as $term) { //this is a line 78 $content .= '<a href="'. $term->slug .'">'. $term->name .'</a>'; } return $content; } – MichaelNagval Oct 21 '14 at 8:40
  • Would you please paste your full class-dportfolio.php code in pastebin and send the link? – Ashok Kumar Nath Oct 21 '14 at 9:25
  • Thnx you very much! pastebin.com/Wp4Cypkx – MichaelNagval Oct 21 '14 at 11:41
  • First of all, are you sure the taxonomy name is dportfolio_categories? If yes, what do you see if you print $terms in line 76 and print the global $post in line 74? Like this: pastebin.com/fBuFFp0v This will make sure if $terms is really an array. – Ashok Kumar Nath Oct 21 '14 at 15:20

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.