php - Changing the category list order in Wordpress -
i working on editing wordpress plugin file.
the code below lists categories in alphabetical order. want list them category id, in ascending order.
<?php foreach($categories $category){ $rand = rand(0,99); $catname = get_cat_name( $category ); $find = array("&", "/", " ","amp;","&"); $replace = array("", "", "", "",""); $catname = str_replace($find,$replace,get_cat_name( $category )); ?> <li> <a href="#fragment-<?php echo $catname; ?>"><?php echo get_cat_name( $category ); ?></a> </li> <?php } ?>
i tried working get_cat_id
function, i'm not programmer got stuck. please help.
rather querying categories inside theme file, better way register custom function in wordpress theme.
googling "wordpress category order id" give me link
https://wordpress.org/support/topic/how-to-order-my-categories-by-id-instead-of-name. answer there good.
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; }
you can call function in theme files return categories ordered id. that:
<?php $categories = get_the_category_sort_by_id($categories); foreach($categories $category){ ... ?> <li> <a href="#fragment-<?php echo $catname; ?>"><?php echo get_cat_name( $category ); ?></a> </li> <?php } ?>
Comments
Post a Comment