Skip to content

Instantly share code, notes, and snippets.

@Javlopez
Created July 29, 2011 04:08
Show Gist options
  • Save Javlopez/1113111 to your computer and use it in GitHub Desktop.
Save Javlopez/1113111 to your computer and use it in GitHub Desktop.
Sencillo script que permite extraer categorias y subcategorias de wordpress a un array
<?php
/**
*@example $array_categorys = get_array_categorys();
*/
function get_array_categorys()
{
$categories = get_categories(array('type' => 'post','orderby' => 'name','order' => 'ASC','hide_empty' => false));
$wp_cat = array();
//Get categorys parents
foreach($categories as $category){
if($category->parent=="0" AND $category->slug != "uncategorized")
{
$wp_cat[$category->term_id] = array(
"id" => $category->term_id,
"name" => $category->name,
"slug" => $category->slug,
"subcategorys" => array(),
);
}
}
//get subcategorys
foreach($categories as $category){
if($category->parent!="0" AND $category->slug != "uncategorized")
{
$subcategory = array(
"id" => $category->term_id,
"name" => $category->name,
"slug" => $category->slug,
);
array_push($wp_cat[$category->parent]["subcategorys"],$subcategory);
}
}
return $wp_cat;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment