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>
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>
Share
Improve this question
edited Sep 20, 2019 at 11:18
MaddoxSt
asked Sep 18, 2019 at 6:11
MaddoxStMaddoxSt
138 bronze badges
1 Answer
Reset to default 0If 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 );