WordPress.org

Ready to get started?Download WordPress

Forums

Categories and Subcategories (7 posts)

  1. starbug72
    Member
    Posted 9 years ago #

    The default WP template lists all categories and subcategories in the right column.

    How do I make it list only top level categories and NOT subcategories?

    Or list top level category and then an indention for all subcategories?
    example

    Category A
    - subcategory 1
    - subcategory 2
    Category B
    - subcategory 1
    - subcategory 2

  2. Kafkaesqui

    Posted 9 years ago #

    http://codex.wordpress.org/Template_Tags/wp_list_cats

    No child categories:

    <?php wp_list_cats('children=0'); ?>

    If subcategories are not indenting, then you're setting the 'list' or 'hierarchical' parameter to 0 in wp_list_cats(), or something in your css is reformatting the category listings.

  3. starbug72
    Member
    Posted 9 years ago #

    Thanks for the tip! I updated the code to be <?php wp_list_cats('children=1'); ?> and now it's listing subcategories under the main cats.

    Only problem now is that the cats are not in alphabetical order. Any ideas?

    Thanks in advance.

  4. Kafkaesqui

    Posted 9 years ago #

    "Only problem now is that the cats are not in alphabetical order."

    <?php wp_list_cats('children=1&sort_column=name'); ?>

    That should do it.

  5. starbug72
    Member
    Posted 9 years ago #

    You are awesome, thanks!!!

    Last questions...promise! What if I want categories in a certain order that's not alpha?

  6. Kafkaesqui

    Posted 9 years ago #

    "Last questions...promise!" I've heard that one before...

    You have several options, all limited in one way or another:

    1. Rewrite the underlying code for list_cats (wp_list_cats is what's known as a "wrapper" to this function). Obviously a choice for PHP-heads.

    2. Rename your categories in some fashion to fit the order you want them in. I've never liked this one.

    3. In the database, under the wp_categories table, rearrange the cat_IDs to the way you want your categories listed, then use 'sort_column=ID'. With this goes the standard warning: back up your database before making changes.

    4. Hope someone made a plugin that does what you want. Haven't seen one like this.

    5. Write the plugin. Again, PHP-head territory.

  7. starbug72
    Member
    Posted 9 years ago #

    Thanks for suggesting the various options. I considered both the alpha and changing the IDS. As you said, both have problems. There's only 8 main cats, but some have many subcats (one has 50 sub cats) and with that many, the subcats need to be alpha.

    Rearranging the IDs in the table would work -- unless I want to add another subcat later and have it be alpha. Then I'd have to go through that renaming process again, and that would be a headache IMO.

    So to make a long story short, I just renamed a couple of the main cats, used the sort by name (alpha) and the order is more to my liking now. This seems to be the simplest and most flexible solution.

    Thanks for the help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.