admin管理员组文章数量:1278792
I would like to get_the_category inside the_post_navigation. I created a custom field on Categories to assign a hex color to each category. I would like to use this color as the next / prev link background color when hovering on the post navigation.
How can I get the id of the category of the "next" or "prev" post?
I should clarify that every post has only two categories and I am only trying to get the id of the second category.
I would like to get_the_category inside the_post_navigation. I created a custom field on Categories to assign a hex color to each category. I would like to use this color as the next / prev link background color when hovering on the post navigation.
How can I get the id of the category of the "next" or "prev" post?
I should clarify that every post has only two categories and I am only trying to get the id of the second category.
Share Improve this question edited Feb 1, 2018 at 15:47 whakawaehere asked Feb 1, 2018 at 15:41 whakawaeherewhakawaehere 7910 bronze badges 4 |1 Answer
Reset to default 0Here was my final solution:
$next_post = get_next_post();
if (!empty( $next_post )){
$next_categories = get_the_terms($next_post->ID,'category');
$next_cat_data = get_option('category_'.$next_categories[1]->term_id);
if (isset($next_cat_data['hex'])){
echo "<style>div.nav-next:hover{background-color:".$next_cat_data['hex'].";} div.nav-next:hover span.post-title, div.nav-next:hover span.meta-nav {color:#f8f7f4;}</style>";
}
}
$previous_post = get_previous_post();
if (!empty( $previous_post )){
$prev_categories = get_the_terms($previous_post->ID,'category');
$prev_cat_data = get_option('category_'.$prev_categories[1]->term_id);
if (isset($prev_cat_data['hex'])){
echo "<style>div.nav-previous:hover{background-color:".$prev_cat_data['hex'].";} div.nav-previous:hover span.post-title, div.nav-previous:hover span.meta-nav {color:#f8f7f4;}</style>";
}
}
本文标签: categoriesGet category id of nextprev post inside thepostnavigation
版权声明:本文标题:categories - Get category id of nextprev post inside the_post_navigation 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741297157a2370883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$next_post = get_next_post(); var_dump($next_post); $cats= get_the_terms($next_post->ID) var_dump($next_post); exit;
But what happens if there's more than one category on a post? – admcfajn Commented Feb 2, 2018 at 0:33