28. October 2011
von Blackbam

This little scripts help ordering WordPress categories by meta values – a thing which is not integrated into WordPress yet and which costs some time to integrate.

1. Create a meta value (or meta values) for categories to order by

To order categories by a meta value the first thing which we have to do is to create a custom category meta value to order these (this first script is adapted from bainternet.com, where it is demonstrated in more detail).

 

//add extra fields to category edit form hook
add_action ( 'add_category_form', 'extra_category_fields');
add_action ( 'edit_category_form_fields', 'extra_category_fields');
 
//add extra fields to category edit form callback function
function extra_category_fields( $tag ) {    //check for existing featured ID
 
    $t_id = $tag->term_id;
    $cat_meta = get_option("category_$t_id");
?>
 

	
	
	
		

2. Order the categories by the created meta value and display these

// get the categories
$categories = get_categories();
 
$sortar = array();
foreach($categories as $ct) {
	$term_id = $ct->term_id;
	$cat_meta = get_option( "category_$term_id");
	array_push($sortar,array(intval($cat_meta['cat_order_val']),$ct));
}
 
uasort($sortar,create_function('$a,$b','return ($a[0]<=$b[0]) ? -1 : 1;'));
 
$ordered_categories = array();
 
foreach($sortar as $sor) {
	array_push($ordered_categories,$sor[1]);
}
 
foreach($ordered_categories as $oc) {
	// do whatever you want with your category objects
}

Have fun and be free to use these scripts. Improvement ideas are welcome.

Share

Warning: Undefined variable $time_since in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 42 Dieser Eintrag wurde am 28. October 2011 um 0:00 in der Kategorie WP Scripts veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.


Tags:

Fatal error: Uncaught Error: Undefined constant "Ext_related_links" in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php:75 Stack trace: #0 /home/.sites/609/site1266/web/blackbams-blog/wp-includes/template-loader.php(106): include() #1 /home/.sites/609/site1266/web/blackbams-blog/wp-blog-header.php(19): require_once('/home/.sites/60...') #2 /home/.sites/609/site1266/web/blackbams-blog/index.php(17): require('/home/.sites/60...') #3 {main} thrown in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 75 WordPress › Error

There has been a critical error on this website.

Learn more about troubleshooting WordPress.