Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'd like get_the_category_list to only display one or two categories instead of all the categories associated with the post. Haven't been able to find any results.

<?php echo get_the_category_list(); ?>

Any help would be appreciated

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Quick idea would be to pass some simple separator like comma and cut from the start of result till it.

But I think that if you want better control on output it would make more sense to use level deeper get_the_category() function and build markup yourself.

share|improve this answer

If you want to limit get_the_category to any number you can use break;

For example if you want to limit echoing a category + link to show say only 5 categories.

     $i = 0;
      foreach((get_the_category()) as $cat) {
        echo '<a href="'.get_category_link($cat->cat_ID).'"> | ' . $cat->cat_name . '</a>';
        if (++$i == 5) break;
      } 
share|improve this answer

This worked. Found in http://codex.wordpress.org/Function_Reference/get_the_category

<?php
$category = get_the_category(); 
echo $category[0]->cat_name;
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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