• Resolved incredibledad

    (@incredibledad)


    Hi all,

    As per the title, at https://incredibledad.com, I’m trying to order my categories in my main menu bar by count and it’s only letting me order it by name. I’ve tried changing it to ID but no luck.

    Help please and many thanks in advance!

    ID

    <!-- begin categories -->
    		<!-- Category Nav Starts -->
    			<div id="cat_navi" class="wrap">
    				<ul id="secnav">
    
    					<?php if (get_option('pov_home_link')) : ?>
    
    					<?php endif; ?>
    
    					<?php foreach ( (get_categories('exclude='.get_option('pov_cat_ex') ) ) as $category ) { if ( $category->category_parent == '0' ) { ?>
    
                        <li>
                            <a href="<?php echo get_category_link($category->cat_ID); ?>"><?php echo $category->cat_name; ?><br /> <span><?php echo $category->category_description; ?></span></a>
    
                            <?php if (get_category_children($category->cat_ID) ) { ?>
                            <ul><?php wp_list_categories('orderby=count&title_li&child_of='  . $category->cat_ID); ?></ul>
                            <?php } ?>
                        </li>
    
    					<?php } } ?>
    
    				</ul>
    			</div>
    			<!-- Category Nav Ends -->
    		<!-- end categories -->
Viewing 6 replies - 1 through 6 (of 6 total)
  • MichaelH

    (@michaelh)

    What about using the orderby=count argument with the template tag, wp_list_categories()?

    Thread Starter incredibledad

    (@incredibledad)

    I actually have it in there. Is it in the wrong spot?

    Thanks in advance!

    ID

    MichaelH

    (@michaelh)

    Actually it looks like you are using get_categories for most of your categories and that doesn’t offer a sort by count. You’d have to use wp_list_categories or put results of get_categories in an array and sort that by count.

    Thread Starter incredibledad

    (@incredibledad)

    Eeek… I`m a trial and error coder with no formal training.

    Would you be able to give me the code that would work (replace get_categories section with strictly a wp_list_categories section) or show me the code to put the results of get_categories into an array and how to sort that by count?

    Many thanks Michael!

    ID

    MichaelH

    (@michaelh)

    Actually learned that you can sort categories by count when using get_categories…so change this line:

    <?php foreach ( (get_categories('exclude='.get_option('pov_cat_ex') ) ) as $category ) { if ( $category->category_parent == '0' ) { ?>

    to

    <?php foreach ( (get_categories('orderby=count&order=ASC&exclude='.get_option('pov_cat_ex') ) ) as $category ) { if ( $category->category_parent == '0' ) { ?>

    Thread Starter incredibledad

    (@incredibledad)

    Fantastic Michael H!!!

    Thanks for the solution. Just changed order=DESC to get my most prolific categories first.

    Huge help and have a great day!

    ID

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can’t get wp_list_categories to list by count… help!’ is closed to new replies.