The MU forums have moved to

Output wp_list_categories() to variable (2 posts)

  1. JMF
    Posted 16 years ago #

    I've been trying to grab the output of wp_list_categories() in a variable so I could manipulate it some more before the final output.

    I've followed suggestions here: forum post

    and when that didn't work, here: Ideas post

    No luck both times.

    The hack seems straightforward enough. Here's what my wp_list_categories() in category-template.php looks like right now:

    function wp_list_categories($args = '') {
     if ( is_array($args) )
      $r = &$args;
      parse_str($args, $r);
      $defaults = array('show_option_all' => '', 'orderby' => 'name',
        'order' => 'ASC', 'show_last_update' => 0, 'style' => 'list',
        'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1,
        'child_of' => 0, 'feed' => '', 'feed_image' => '', 'exclude' => '',
        'hierarchical' => true, 'title_li' => __('Categories'), 'echo' => 1);
      $r = array_merge($defaults, $r);
    ... no changes here ...
    //echo apply_filters('wp_list_categories', $output);
    ////old statement removed
        return apply_filters('wp_list_categories', $output);
        echo apply_filters('wp_list_categories', $output);

    In my header.php code, I have a call like this:

    <?php $cats = wp_list_categories('sort_column=id&depth=1&title_li=&exclude=1'); ?>

    but the categories still display.

    Am I doing something wrong? Has anyone else tried this?


  2. JMF
    Posted 16 years ago #

    Grr. I got it, sorry. Wasn't being careful.

    One must add "echo=0" to the list of parameters in the wp_list_categories() call, I notice. Problem solved.

About this Topic