Generate array of custom taxonomies using wp_list_categories in WordPress

Here is quick, but quite lengthy technique to generate array of custom taxonomies in WordPress.

Copy the following code in functions.php of your current WordPress’s theme.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//Create a Walker to change the way wp_list_categories displays the output
//Here I'm removing the links from the output
class MyWalker extends Walker_Category {
 
    function start_el(&$output, $category, $depth, $args) {
        extract($args);
        $cat_name = esc_attr( $category->name );
        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $link = $cat_name;
        
            if ( !empty($current_category) ) {
                $_current_category = get_term( $current_category, $category->taxonomy );
                if ( $category->term_id == $current_category )
                    $class .=  'current-cat';
                elseif ( $category->term_id == $_current_category->parent )
                    $class .=  'current-cat-parent';   
            }
            $output .= '*' . $link . '*';//Using "*" as delimiter
        
    }
}


function wp_list_categories_array($taxonomy) {

    $orderby      = 'name';
    $show_count   = 0;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 1;      // 1 for yes, 0 for no
    $title        = '';
    $empty        = 0;
    $output       = array();
    
    $MyWalker = new MyWalker(); // Create a new instance of MyWalker
    
//Prepare the argument for wp_list_categories
    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title,
      'hide_empty'   => $empty,
      'walker'       => $MyWalker
    );
    
    ob_start(); // This will turn off output buffer
    
    wp_list_categories($args); //WordPress function to retrieve categories
    
    $output = ob_get_contents(); //Get the content of output buffer
    
    ob_end_clean(); // forbidden output buffer to echo content
    ob_end_flush(); // flush the buffer

        //Trim the content from delimiter using reg expression
    if(preg_match_all('/\*(.*?)\*/',$output,$match)) {            
            $output = $match[1];             
    }; 
    
    return $output; 

 
}

Code Usage:

1
2
3
4
5
6
<?php
//Generate an array of custom taxonomy
  $arr = wp_list_categories_array("custom-taxonomy-name");//Feed your taxonomy to generate array
  
  print_r($arr);
?>

Enjoy!! Share your code and thoughts…

More Methods


facebooktwittergoogle_plusredditpinterestlinkedinmail

SIGN UP TO OUR NEWSLETTER

Stay tuned with latest updates on Web technologies, subscribe to our newsletter. We'll not spam your inbox.

Speak Your Mind

dialog-information.png

Code highlighting instructions

Use {code type=language} for code highlighting. For example, if you are using Javascript code then use the following to wrap.

{code type=javascript}
//Your code here
{/code}

or for PHP, use:

{code type=PHP}
// Your code here
{/code}

 

*

CommentLuv badge

Security Code