WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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? – tfrommen 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

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

$X = 3; // for instance
$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) {
    $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


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.