0

How do i go about making the code only show one category?

    <?php
  $categories = get_the_category();
  foreach($categories as $key => $category) {
    $url = get_term_link((int)$category->term_id,'category');
    $categories[$key] =
      "<dt><h1>{$category->name}</h1> </dt>" .
      "<dd>{$category->category_description}</dd>";
  }
  echo "<dl>\n" . implode("\n",$categories) . "\n</dl>";
?>

The Reason why am asking is that I have two category(s) on some of the posts but i only want to show one of them. Is there way to add it to the code above so it only shows one thanks.

  • As long as you are looping trough all the categories you are getting them all, so don't do that or skip after the first run. – Nicolai Jun 5 '14 at 11:53
  • Well it will only show the number of category that the post has it wont show all. But i only wanted it to only show one specific one. – H3r0k0 Jun 5 '14 at 11:55
  • are you sure it won't show all humanly possible and thinkable categories? of course I'm kidding here.. ;) – Nicolai Jun 5 '14 at 12:01
  • Sure, my pleasure. – Nicolai Jun 5 '14 at 12:05
1

Thank you for your question.

Try using the following code.

<?php
  $categories = get_the_category();
  if($categories[0]) {
    $url = get_term_link((int)$categories[0]->term_id,'category');
    $temp = "<dt><h1>{$categories[0]->name}</h1> </dt><dd>{$categories[0]->category_description}</dd>";
      echo "<dl>\n" . $temp . "\n</dl>";
  }
?>

Best Regards,

Vinod Dalvi

| improve this answer | |
  • Thanks. This is what i was looking for cheers. Thank you. – H3r0k0 Jun 5 '14 at 11:59
  • 3
    @H3r0k0 You are most welcome and feel free to ask question if you face any. Don't forget to accept answer if it works for you :) – Vinod Dalvi Jun 5 '14 at 12:01

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.