Search code examples
wordpresswp-list-categories

How to show categories without li tags in wordpress?


I need some help with the wordpress function Wp_list_categories() I want to display the categories links inside a tag <div> like this:

<div class="myclass">
  <a>a category</a>
</div>

If I use wp_list_categories() I get the category links wrapped in li tags which also show the bullet ◦ before each category.

i wonder if there's any method like this:

wp_get_archives('format=custom type=monthly &before=<div class="voices">&after=</div>');

that gives me:

<div class="voices">
  <a>something</a>
</div>

Solution

  • You can use wp_get_post_categories(): https://developer.wordpress.org/reference/functions/wp_get_post_categories/

    Example:

    <?php
        $categories = wp_get_post_categories(get_the_ID());
    
        echo '<div class="voices">';
        foreach($categories as $category){
            echo '<a>' . get_cat_name($category) . '</a>';
        }
        echo '</div>';
    ?>