Forums

[resolved] Display 1 category w/ link to it (5 posts)

  1. draclan12
    Member
    Posted 3 years ago #

    How can I get WordPress to display a link to only ONE category that a certain post has which links to show all posts of that category?

    For example, if I had a post with the categories of "News, Cold, & Freezing", it would go and grab the first category (News) and display it. Then it would put a link around it so, when clicked, I can view other posts of that category.

    <?php $category = get_the_category(); echo $category[0]->cat_name; ?>
    ^ That does one category, but I need how to do the link part..

  2. @mercime
    Member
    Posted 3 years ago #

    How can I get WordPress to display a link to only ONE category that a certain post has which links to show all posts of that category?

    What theme are you using? Usually when you click on a category link, all the posts under that category will show up using the archive.php or category.php
    OR, you can create your own category template
    http://codex.wordpress.org/Category_Templates

  3. draclan12
    Member
    Posted 3 years ago #

    What theme are you using? Usually when you click on a category link, all the posts under that category will show up using the archive.php or category.php
    OR, you can create your own category template
    http://codex.wordpress.org/Category_Templates

    I'm making my own theme. I know if you put this:

    <?php the_category(' '); ?>

    It'll display all the categories of that post (with links), but I want it to display only one category (with a link to it).

  4. @mercime
    Member
    Posted 3 years ago #

    Display only ONE category with a link to it - used within the loop:
    <?php $cat = get_the_category(); $cat = $cat[0]; ?><a href="<?php echo get_category_link($cat->cat_ID);?>"><?php echo $cat->cat_name; ?></a>

  5. draclan12
    Member
    Posted 3 years ago #

    Thanks a lot, that works perfectly.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags