0

So currently i have a taxonomy named 'blog-category' which contains the categories of the custom post type 'blogs'. On my home page i have posts that show their respective categories with them. I am getting super confused to how to get the link of the category as per the categories name. Here is how i am currently displaying the categories on the blog page. I have a function

function blog_categories_terms($postID, $term){
    $terms_list = wp_get_post_terms($postID, $term);
    $output = '';
    foreach ($terms_list as $term) {
                $output .=  $term->name.' ';
    }
    return $output;
}

I currently only display the categories name currently. I need help to get the respective URL for each category Here is the front end code-

<div class="blog-cat">
  <a href="">
     <label for=""><?php echo blog_categories_terms($post->ID, 'blog- category');?></label>
  </a>
</div>

So if my category is WHO on the post it should also have a link for WHO and so on. How do i do that?

Here is the worked out solution for people who might be looking: This is my function

function blog_categories_terms($postID, $term){
    $terms_list = wp_get_post_terms($postID, $term);
    $output = '';
    foreach ($terms_list as $term) {
                $cat_url = get_term_link( $term );
                $output .=  $term->name.' ';
    }
    $category_detils = array('cat_url' => $cat_url, 'output' => $output );
    return $category_detils;
}

This is my function call for getting the url and the title

<a href="<?php
$to_send = blog_categories_terms($post->ID, 'blog-category');
echo $to_send['cat_url'];; ?>">
    <label for="">
    <?php
        $to_send = blog_categories_terms($post->ID, 'blog-category');
        echo $to_send['output'];; ?>
    </label>
</a>
0

If you want to display links to each category, separated by spaces, use get_the_term_list():

<?php echo get_the_term_list( null, 'blog-category', '', ' ', '' ); ?>

If you have an individual term whose link you want to get, use get_term_link():

$url = get_term_link( $term );
  • So i think i put my question wrong or something. So what i meant was i have lets say categories of Who, What and Where right So there are posts that have 1 category of the 3 categories. So 3 post of Who, 3 post of Where and 3 of What. And i am currently displaying all the posts in random and then when i click the Category then i need to get to the correct Category's link . If i click Who it should go to who, where to Where's link and what to What's link depending as per the post clicked. – MaddoxSt Sep 18 at 6:25
  • get_the_term_list() displays all the category names associated with the current post in the loop, and each one is linked to the archive for that category. Is that not what you want? – Jacob Peattie Sep 18 at 6:30
  • I need the link for each Post displayed's category. For Example: My home page has 3 post right Post 1 : Post with Category Who Post 2 : Post with Category What Post 3 : Post with Category Who So if i click Post 1's category then it should go to Who, If i click Post 2's Category it should go to What and If i click Post 3's category it should go to Who page. – MaddoxSt Sep 18 at 6:33
  • That's exactly what I said. – Jacob Peattie Sep 18 at 6:41
  • Thanks a lot it took me a bit to reply but yeah i figured it out. I have added a bit of code on my question for the full answer to whom might be looking out later. – MaddoxSt Sep 20 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.