-1

I bought a wordpress theme and I am not able to display the category link and name.

<div class="item-info">';
  if($show_aut!='0'){
      $author = get_author_posts_url( get_the_author_meta( 'ID' ) );
      $html .='<span class="item-author"><a href="'.$author.'"title="'.get_the_author().'">'.get_the_author().'</a></span>';}

  //DISPLAY CATEGORY LINK AND NAME
  if($show_sub_aut!='0'){
      $subaut = get_the_category();
      $html .= '<span  class="item-sub-aut"><a href="'.$subaut.'" title="'.get_category_link().'">'.get_category_link().'</a></span>';}
  if($show_date!='0'){  
   $html .= '<span class="item-date">'.get_the_time(get_option('date_format')).'</span>';}                    
   $html .= '</div>
   </div>';

closed as off-topic by TheDeadMedic, Michael, Howdy_McGee, EAMann Mar 7 '16 at 20:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Hey Pieter, I cleaned the code. Can you take a look now. Thanks. – Mark_123 Mar 1 '16 at 10:34
  • Much better, and much more readable. It really helps to properly indent code. It is easy to see the issue now without even looking – Pieter Goosen Mar 1 '16 at 10:39
1

get_the_catgory() returns an array of category objects assigned to a post. You are trying to use the result as a string. If you have debugging enabled, you would have received an error regarding this.

You either need to loop through the array and handle each category separately, or you can reference them directly, like get_the_category()[0] would return the first category object

Lets say you would want to show only the first category, then the following line

$html .= '<span  class="item-sub-aut"><a href="'.$subaut.'" title="'.get_category_link().'">'.get_category_link().'</a></span>';

would become

$html .= '<span  class="item-sub-aut"><a href="'. esc_url( get_category_link ( $subaut[0] ) ) .'" title="'. $subaut[0]->name.'">' . $subaut[0]->name . '</a></span>';

EDIT

If you need to display all the categories separated by a comma, you can do the following

$subaut = get_the_category();
foreach ( $subaut as $cat ) 
    $array[] = '<span  class="item-sub-aut"><a href="'. esc_url( get_category_link ( $cat ) ) .'" title="'. $cat->name.'">' . $cat->name . '</a></span>';

$html .= implode( ', ', $array );
  • You are a genius the code works great. I wish all the best and much success. Before I close I wanted to say a last thing, I have post that belong to 2 categories, but now it is displaying only one, how can I get to display all the categories separated by commas. Thanks. – Mark_123 Mar 1 '16 at 11:02
  • Answer updated. It is untested though, but should work. – Pieter Goosen Mar 1 '16 at 11:15
  • I get an Parse error: syntax error, unexpected '$array' (T_VARIABLE) – Mark_123 Mar 1 '16 at 11:22
  • Sorry, missed a , in the last line. I have updated the code. Should work now – Pieter Goosen Mar 1 '16 at 11:27
  • Once again, thanks a lot. I wish you all the best and much success. You are a genius. Thanks. – Mark_123 Mar 1 '16 at 11:32
1

Get the category return an array, use

 <?php 
foreach((get_the_category()) as $cat) { 
// print_r(cat);
} ?> 

to get more detail about link and category name

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