WordPress.org

Ready to get started?Download WordPress

Forums

Displaying Category / Slug Name (6 posts)

  1. James182
    Member
    Posted 3 years ago #

    Hi, I was wondering if there is any short snippet of code that allows you to display the current category you are in but also displays a hyphen in the middle, like you find with the permalinks. So if i was in the category "Music News" I could echo out the word "music-news". Any help would be greatly appreciated!!!

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    to be used in category.php (or achive.php)

    <?php echo get_category(get_query_var('cat'))->slug; ?>

  3. Ganners
    Member
    Posted 3 years ago #

    $categories = get_the_category($post->ID);
    print $categories->category_nicename;

    Is that what you're after?

  4. James182
    Member
    Posted 3 years ago #

    @Ganners Thanks for your reply but for some reason it is now showing anything, not even in the source.

    @alchymyth That snippet of code works great!, another quick question is, if i am on the homepage for example, it shows "home" or on the search page it displays "Search", assuming be a if and else statement? (sorry i am learning as go along)

  5. esmi
    Forum Moderator
    Posted 3 years ago #

  6. James182
    Member
    Posted 3 years ago #

    thx for the link esmi, that was really helpfull

Topic Closed

This topic has been closed to new replies.

About this Topic