WordPress.org

Forums

link to category inside post? (3 posts)

  1. sepp88
    Member
    Posted 4 years ago #

    hey guys, i simply want to have a link to the category my current post fits in.

    currently I'm using:

    <?php
    			$category = get_the_category();
    			echo "<span class='catname'>" . $category[0]->cat_name . "</span>";
    			?>

    however, this is just the name of the category. i need a link to the category archive.

    is there a template_tag to use?
    thank you!

  2. shawnyuan
    Member
    Posted 4 years ago #

    Yea you can use this:

    <?php
        // Get the ID of a given category
        $category = get_the_category();
        $category_id = $category[0]->cat_ID;
        $category_name = $category[0]->cat_name; 
    
        // Get the URL of this category
        $category_link = get_category_link( $category_id );
    ?>
    
    <!-- Print a link to this category -->
    <a href="<?php echo $category_link; ?>" title="Category Name"><?php echo $category_name; ?></a>
  3. sepp88
    Member
    Posted 4 years ago #

    thank you, worked perfectly!

Topic Closed

This topic has been closed to new replies.

About this Topic