Support » Fixing WordPress » how to list the top 5 posted in categories only?

  • i tried looking but the codex says they can only be ordered by name or ID number. is there a way to list only the top x number of categories?

    thank you!

Viewing 13 replies - 1 through 13 (of 13 total)
  • <div id="domtabone">
    <h2><?php _e('Noteworthy','k2_domain'); ?></h2>
    <ul>
    <?php $temp_query = $wp_query; query_posts("showposts=10&cat=21"); ?>
    <?php while (have_posts()) { the_post(); ?>
    <li>
    »<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to &ldquo;<?php the_title(); ?>&rdquo;"><?php the_title(); ?></a>
    </li>
    <?php } $wp_query = $temp_query; ?>
    </ul>
    </div>

    This maybe helpful to you.

    Also see:

    http://wordpress.org/support/topic/38046

    If none of this helps, please reply in more detail on what you want to accomplish.

    Thread Starter belledumonde

    (@belledumonde)

    oaso, i see k2 there, does that mean that only works for wordpress sites working with k2? and im not looking to post the titles so im not sure thats exactly what i want but thank you for your input. Kafkaesqui, thanks for the link but its not what im looking for either.

    i think i was a bit vague in describing what i was looking for. very sorry.

    in the codex it says it only arranges categories by alphabetical order or by ID number but i would like to list my categories by order of which have the most posts. then on the homepage, i want to list only the top five.

    is there a simple line of code that can do that? or a plugin maybe?

    thank you for the help.

    What you’re asking for would be a bit more than a line of code, though not *too* much more than that. The following does what you’re looking for:

    <?php
    $limit = 5;
    $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE category_count > 0 ORDER BY category_count DESC LIMIT $limit");
    foreach($categories as $category) :
    ?>
    <li><a href="<?php echo get_category_link($category->cat_ID); ?>"><?php echo $category->cat_name; ?></a> (<?php echo $category->category_count; ?>)</li>
    <?php endforeach; ?>

    Just modify the $limit value to set how many categories to display.

    Thread Starter belledumonde

    (@belledumonde)

    oh wow, it works 🙂 really cool, a couple more lines and you can add it to your list of great plugins! thanks!

    This is great, very close to what I need.

    How can I limit the list of links to within individual categories, instead of all categories?

    Please expand on that one, stinker. Do you mean you want an ‘exclude’ option?

    I inserted your code and it displays categories, yes indeed. Thank you. But I want to list subcategories instead of main categories.

    I guess I want the equivalent of the “child=” in wp_list_cats

    Well…

    Child categories in general:

    <?php
    $limit = 5;
    $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE category_count > 0 AND category_parent <> 0 ORDER BY category_count DESC LIMIT $limit");

    Child categories for a specific (main) category:

    <?php
    $limit = 5;
    $parent_cat = 100;
    $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE category_count > 0 AND category_parent = '$parent_cat' ORDER BY category_count DESC LIMIT $limit");

    Modify the value of $parent_cat to the (correct) category ID.

    Child categories for *current* category:

    <?php
    $limit = 5;
    $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE category_count > 0 AND category_parent = '$cat' ORDER BY category_count DESC LIMIT $limit");

    And finally
    Child categories for *current* category OR siblings of a (parent) category when in a child category:

    <?php
    $limit = 5;
    $category = $wp_query->get_queried_object();
    $parent = ($category->category_parent) ? $category->category_parent : $cat;
    $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE category_count > 0 AND category_parent = '$parent' ORDER BY category_count DESC LIMIT $limit");

    A final note: When asked to expand on a request, we really do mean expand. Providing as much detail as possible on exactly what results you want are of major importance.

    [This one’s not my fault, Kafkaesquí….]

    Well, at least it’s not another authors/users issue…

    Sorry about that; I guess I did the opposite of “expand”.

    This is definitely what I’m looking for.

    Let me expand (after the fact)… on most of my pages, I want the complete list of subcategories within each category. But on my main index page, I only want 8 subcategories in each category menu. As a bonus, it would be nice to alphabetize those 8, but I’ll settle for however it comes out.

    Thank you Kafakesqui!! Somehow I knew you were the person who would help me out.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘how to list the top 5 posted in categories only?’ is closed to new replies.