admin管理员组文章数量:1317572
So, I'm having an issue where using the get_the_category function is returning an incorrect slug. My code snippet is as so:
<?php $category_detail=get_the_category($post->ID);
foreach($category_detail as $cd){?>
<li class="list-inline-item"><a href="<?php echo $cd->slug ?>"><?php echo $cd->cat_name; ?></a>
</li><?php }
The issue is that the category slug doesn't reflect my site's permalink settings, and returns: "mysite/category-name"
and not "mysite/category/category-name".
I am not sure why the category permalink is being truncated, as the category pages work correctly, but this function is outputting them incorrectly, The slug in question here is for regular posts.
So, I'm having an issue where using the get_the_category function is returning an incorrect slug. My code snippet is as so:
<?php $category_detail=get_the_category($post->ID);
foreach($category_detail as $cd){?>
<li class="list-inline-item"><a href="<?php echo $cd->slug ?>"><?php echo $cd->cat_name; ?></a>
</li><?php }
The issue is that the category slug doesn't reflect my site's permalink settings, and returns: "mysite/category-name"
and not "mysite/category/category-name".
I am not sure why the category permalink is being truncated, as the category pages work correctly, but this function is outputting them incorrectly, The slug in question here is for regular posts.
1 Answer
Reset to default 2The problem, as I could see it, is not with the get_the_category()
function. Instead, it's the way you output the category archive link: href="<?php echo $cd->slug ?>"
— you should instead use get_category_link()
to get the correct URL of the category archive page. Example:
<a href="<?php echo esc_url( get_category_link( $cd ) ); ?>">
本文标签: categoriesHow to fix getthecategory function returning incorrect slug
版权声明:本文标题:categories - How to fix get_the_category function returning incorrect slug? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742005483a2411869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论