admin管理员组文章数量:1332377
I'm trying to populate the category id from the category name within the loop.
<?php $cat_name = get_theme_mod( 'cat_1' ); ?>
<?php $category_id = get_cat_ID($cat_name); ?>
<?php query_posts('cat='.$category_id); ?>
the cat_1 theme mod is a category dropdown I have in the theme customizer that gets and lists category names in a dropdown list. Currently, my loop is showing all posts from all categories instead of the category selected from theme_mod cat_1.
When I use:
<h4><?php echo $category_id; ?></h4>
it returns as '0' instead of the id of the category when selected from the category dropdown (theme mod cat_1).
I'm trying to populate the category id from the category name within the loop.
<?php $cat_name = get_theme_mod( 'cat_1' ); ?>
<?php $category_id = get_cat_ID($cat_name); ?>
<?php query_posts('cat='.$category_id); ?>
the cat_1 theme mod is a category dropdown I have in the theme customizer that gets and lists category names in a dropdown list. Currently, my loop is showing all posts from all categories instead of the category selected from theme_mod cat_1.
When I use:
<h4><?php echo $category_id; ?></h4>
it returns as '0' instead of the id of the category when selected from the category dropdown (theme mod cat_1).
Share Improve this question asked Oct 30, 2015 at 23:25 user3350469user3350469 113 bronze badges1 Answer
Reset to default 1Answered my own question, although not exactly how I had originally intended.
<?php
$cat_name = get_theme_mod( 'cat_1' );
$cat_name = str_replace(' ','-',$cat_name);
?>
<?php query_posts('category_name=' . $cat_name); ?>
So instead of trying to convert the category name to the category id, this seems to convert the category name to the category slug. I can test this by echoing $cat_name and it returns the category slug and not the category name.
In any case, this works now. Posted so others can use it if they need it.
本文标签: loopCategory ID returns as 39039
版权声明:本文标题:loop - Category ID returns as '0'? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742305932a2449915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论