categories - How to display wp_list_categories on div instead of li?

admin2025-06-06  4

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);

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);
Share Improve this question edited Jul 30, 2015 at 22:36 tfrommen 9,2317 gold badges40 silver badges59 bronze badges asked Jul 30, 2015 at 21:42 HugoHugo 311 gold badge1 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 5

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 );
}

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 :)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749199180a317194.html

最新回复(0)