• Hi There

    I’m looking for a solution to output all corresponding categories per post without the link atttribute as I need it for css styling.

    I’ve tried and fiddled around with get_categories() etc, but this will only display the category with the lates ID, What I need would be to display all categories divided with a space, so I can use it as a css value in a list item such as:

    <li class="cat1 cat3">...</li>
    <li class="cat2">..</li>
    <li class="cat3 cat4">..</li>
    ....

    and so on. Hope its understandable…

    Thanks and regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • <ul>
    <?php while(have_posts()) : the_post(); ?>
    <li class="<?php
    $cats = get_the_category();
    $length = count($cats);
    $i = 1;
    foreach((get_the_category()) as $category) {
    if($length == $i){
    $catString = $category->category_nicename . '';
    } else {
    $catString = $category->category_nicename . ' ';
    }
    echo $catString;
    $i++;
    } ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endwhile; ?>
    </ul>

    see if that will work for you. It should make all the categories a post is in become a class for the <li> that holds the post title. All lowercase FYI.

    Example:

    <ul>
    <li class="dogs cats animals"><a href="">title</a></li>
    <li class="ballons scary-stuff"><a href="">title</a></li>
    </ul>
    Thread Starter mauricenaef

    (@mauricenaef)

    Hi tugbucket

    You saved my day! thanks a Million, this worked just perfect! Funny how this isn’t included in wordpress by default!!
    Anyway thank you so much!

    Kind regards…

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display all Category Names without link’ is closed to new replies.