admin管理员组

文章数量:1122846

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?

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?

Share Improve this question edited Oct 21, 2014 at 6:44 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Oct 21, 2014 at 6:41 MichaelNagvalMichaelNagval 1
Add a comment  | 

2 Answers 2

Reset to default 0

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

if ( class_exists( 'DPortfolio' ) ) {
      echo DPortfolio::instance()->dportfolio_get_category();
}else{
    the_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.

You can achieve this by using an if/else statement along with the has_category() function in wp

if ( class_exists( 'DPortfolio' ) && has_category( 'dportfolio' ) ) {
    echo DPortfolio::instance()->dportfolio_get_category();
} else {
    the_category(', ');
}

本文标签: categoriesthecategory ifelse statement