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.

How can i get only x number of categories using the_category. I triesd a few thing but none worked. I dont want to use get_the_category

share|improve this question
1  
I don't understand what you want to do. the_category and get_the_category do exactly the same, however, the first function prints the cat, the second returns it. So, do you want to A) get or B) print P) X posts of a certain category or Q) X categories of the existing ones, or something else? –  t f Apr 17 '13 at 12:22
    
the reason is that the_category gives me the result in ul li format as well as categories are linked to theris permalinks. I dont want to do iteration using get_the_category and increase unwanted work! –  Sankalp Mishra Apr 17 '13 at 12:29
    
The work, as far as the server is concerned, should be about the same whether you loop through the categories or whether the_category does so your concern about unwanted work may not be relevant, but maybe you are talking about the work that you have to do. –  s_ha_dum Apr 17 '13 at 13:06

1 Answer 1

After reading your question (and comment) again, this could be what you want to do:

$X = 3; // for instance
ob_start();
the_category();
$cats = implode('<li', array_slice(explode('<li', ob_get_clean()), 0, $X+1));

If you want to use this throughout your theme multiple times, use a function. Put the following in your functions.php:

function my_the_category($limit) {
    $limit++;
    ob_start();
    the_category();
    $cats = implode('<li', array_slice(explode('<li', ob_get_clean()), 0, $limit));
}

Now you can use <?php my_the_category(3); ?>, for example.

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.