$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - How to display wp_list_categories on div instead of li?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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

Post a comment

comment list (0)

  1. No comments so far