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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

Answered 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