WordPress.org

Ready to get started?Download WordPress

Forums

the_category as list? (16 posts)

  1. ThreeVisual
    Member
    Posted 6 years ago #

    Hi, Not sure if this is possible but would be fantastic if it is and someone could help me out.

    I'm working on my new blog design and on the single post page I want to show the categories, however I want to show them as a list and it doesnt look like this is possible using 'the_category' tag.

    I am trying to show the categories like this:

    <ul class="postinfo-cat">
    	<li><a href="#">Category 1</a></li>
    	<li><a href="#">Category 2</a></li>
    </ul>

    Is there any other way to do this?

  2. ThreeVisual
    Member
    Posted 6 years ago #

    anyone?

  3. Mithrustt
    Member
    Posted 6 years ago #

    This is what I have. You'll likely have to do some CSS work to get it to look exactly like you want it, though.

    <ul class="categories">
    
    <?php wp_list_categories() ?>
    
    </ul>
  4. ThreeVisual
    Member
    Posted 6 years ago #

    thanks but that lists all my wordpress categories, I just want to display the categories that are applied to one post.

  5. Mithrustt
    Member
    Posted 6 years ago #

    Oh, sorry. I misread. Try this:

    <?php the_category(', '); ?>

  6. ThreeVisual
    Member
    Posted 6 years ago #

    But is there any way I can get the categories to show within a list?

  7. arkane8
    Member
    Posted 6 years ago #

    Try this custom function

    <ul>
    <?php foreach((get_the_category()) as $cat) {
    echo '<li><a href="http://www.yoururl.com/yourpermalinkbasedirectory/'
    . $cat->category_nicename . '">' . $cat->cat_name .
    '</a></li>';
    } ?>
    </ul>
  8. Mithrustt
    Member
    Posted 6 years ago #

    Just put it inside:

    <ul class="categories">

    And then you have to create that class in your style.css and style however you want.

  9. arkane8
    Member
    Posted 6 years ago #

    Mithrustt that doesn't work as it doesn't define <li>
    tags in between each category using the the_category() function.

    Use the function I provided on top, ThreeVisual. It is exactly what you need.

  10. whooami
    Member
    Posted 6 years ago #

    http://codex.wordpress.org/Template_Tags/the_category

    <?php the_category('separator', 'parents' ); ?>

    so if you were going to display a list, what then would you need to separate them with? </li><li> right?

  11. arkane8
    Member
    Posted 6 years ago #

    whooami, that is totally going to cause errors as the very first category in the list will have an unclosed </li> tag, which can mess up an entire display of the page.

    once again:

    <ul>
    <?php foreach((get_the_category()) as $cat) {
    echo '<li><a href="http://www.yoururl.com/yourpermalinkbasedirectory/'
    . $cat->category_nicename . '">' . $cat->cat_name .
    '</a></li>';
    } ?>
    </ul>
  12. whooami
    Member
    Posted 6 years ago #


  13. whooami
    Member
    Posted 6 years ago #

    ffs, do I have to explain THAT ?

    <ul><li><?php the_category('</li><li>'); ?></li></ul>

    I thought that would be self-evident.

  14. arkane8
    Member
    Posted 6 years ago #

    Good point. You got me there. Lol. Not enough coffee today.

  15. whooami
    Member
    Posted 6 years ago #

    indeed. I just screwed up my own fricken feed to make sure it worked.

    <a href="http://www.village-idiot.org/archives/2008/06/22/categories/" rel="bookmark" title="Permanent Link: categories">categories</a></h2>
    <ul><li><a href="http://www.village-idiot.org/archives/category/code/" title="View all posts in code" rel="category tag">code</a></li><li> <a href="http://www.village-idiot.org/archives/category/coconut-grove/" title="View all posts in coconut's grove" rel="category tag">coconut's grove</a></li><li> <a href="http://www.village-idiot.org/archives/category/bushwatch/" title="View all posts in bushwatch" rel="category tag">bushwatch</a></li><li> <a href="http://www.village-idiot.org/archives/category/get-squirly/" title="View all posts in get squirly" rel="category tag">get squirly</a></li></ul>

    it does.

  16. ThreeVisual
    Member
    Posted 6 years ago #

    Cheers for your help... I seem to have it working :)

Topic Closed

This topic has been closed to new replies.

About this Topic