2

Does anyone know how to display the wp_list_categories() on a div instead of the li?

I basically want to wrap main categories and its children in a bootstrap column.

$args = array(
    'taxonomy'           => 'product_category',
    'hide_empty'         => 0,
    'orderby'            => 'name',
    'order'              => 'ASC',
    'show_count'         => 0,
    'use_desc_for_title' => 0,
    'title_li'           => 0
);
wp_list_categories($args);
3

Please add two new argument into your $args array.

1) Style with none value. See the markup section for more. 2) echo with 0(False).

Now call and store the result into $categories variable and print it via printf().

Final code like:

$args = array(
    'taxonomy'           => 'product_category',
    'hide_empty'         => 0,
    'orderby'            => 'name',
    'order'              => 'ASC',
    'show_count'         => 0,
    'use_desc_for_title' => 0,
    'title_li'           => 0,
    'style'              => 'none',
    'echo'               => 0,
);

$categories = wp_list_categories($args);
if ( $categories ) {
    printf( '<div>%s</div>', $categories );
}

In aditional, you can use if ( !preg_match( '/No\scategories/i', $cats ) ) If the displayed text reads "No categories".

P.S. If this stuff helps you please leave me a comment and support :)

3

You can specify the style argument as something other than the default (which is list) and it won't wrap the output in a <li>. You can then wrap it in a <div> yourself.

Combine it with the echo argument if you need to check that the list isn't empty. Example:

$args = array(
    'taxonomy'           => 'product_category',
    'hide_empty'         => 0,
    'orderby'            => 'name',
    'order'              => 'ASC',
    'show_count'         => 0,
    'use_desc_for_title' => 0,
    'title_li'           => 0,
    'style'              => '',
    'echo'               => false,
);
$categories = wp_list_categories($args);

if ( $categories ) {
    printf( '<div class="col">%s</div>', $categories );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.