WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to order my Categories by ID instead of Name (7 posts)

  1. jboepple
    Member
    Posted 1 year ago #

    I know there are a million posts of the same topic, but since everyone's themes are coded differently, I haven't been able to find the answer to my problem.

    My theme is listing my categories in alphabetical order because that's the default. I've gone into the editor to see where I can change "name" to "id" but there isn't any code in there for it.

    I don't know what to add to make it work. I've seen many solutions use the $arg line of code, and I don't see that anywhere, so maybe that's the problem.

    Can someone tell me what to add? Thank you :)

    <?php
    			/* translators: used between list items, there is a space after the comma */
    			$categories_list = get_the_category_list( __( '&nbsp; // &nbsp;', 'twentyeleven' ) );
    
    			/* translators: used between list items, there is a space after the comma */
    			$tag_list = get_the_tag_list( '', __( ', ', 'twentyeleven' ) );
    			if ( '' != $tag_list ) {
    
    			} elseif ( '' != $categories_list ) {
    				$utility_text = __( '<span style="font-family: Crimson Text, serif; font-style: italic; font-size: 16px;" class="entry-utility-prep entry-utility-prep-cat-links">Posted in:&nbsp;</span> %1$s', 'twentyeleven' );
    			}
    
    			printf(
    				$utility_text,
    				$categories_list,
    				$tag_list,
    				esc_url( get_permalink() ),
    				the_title_attribute( 'echo=0' ),
    				get_the_author(),
    				esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) )
    			);
    		?>

    `

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    try to add this filter code to functions.php of your theme:

    add_filter('get_the_categories','get_the_category_sort_by_id');
    
    function get_the_category_sort_by_id( $categories ) {
    
      usort($categories, '_usort_terms_by_ID');
    
      return $categories;
    }

    (not widely tested)

  3. jboepple
    Member
    Posted 1 year ago #

    I put that at the bottom of the functions.php file and I got a Parse Error:

    Parse error: syntax error, unexpected T_FUNCTION, expecting T_STRING or T_VARIABLE or '$' in /var/www/ti/tierslovejoy.com/public_html/wp-content/themes/custom/functions.php on line 616

    Any other suggestions?

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    the error is not likely to be caused by the new code;

    check the coding of your functions.php;

    or post the full code of functions.php which is causing the error - please use the pastebin - http://codex.wordpress.org/Forum_Welcome#Posting_Code

  5. jboepple
    Member
    Posted 1 year ago #

    thank you alchymyth, this is my first time posting on the codex. I have a friend I usually bug for help but I'm needing help more frequently. Pastebin is awesome.

    Here's the link to my functions.php code. I've placed your suggested code at the bottom of the file.

    http://pastebin.com/V3BnRqDy

  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    seems that whatever text editor you have used to edit functions.php, has changed the single quotes ' to html entities '

    try to use the dashboard - appearance - editor to add the code:

    add_filter('get_the_categories','get_the_category_sort_by_id');
    function get_the_category_sort_by_id( $categories ) {
      usort($categories, '_usort_terms_by_ID');
      return $categories;
    }
  7. jboepple
    Member
    Posted 1 year ago #

    it worked! thank you so much for your help alchymyth, you're my hero!

Topic Closed

This topic has been closed to new replies.

About this Topic