treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] How to Get Category-ID or Category Slug Inside the query_post String? (Wordpress)

  • Hi, I've been reading a lot and can't find a solution for this problem.

    I have several category pages in my Wordpress website and I want to use ONLY ONE TEMPLATE for all those category pages. And by the way, I've created pages to manage all the sections/categories of my website more easily.

    I know I can use the next code for every single page template and change the slug:

    <?php query_posts( array ( 'category' => 'whatever_category'); ?>
    

    Is there any way to make it so 'whatever_category' is got from the current category or custom field so I don't have to create page templates for every single category page?

  • Maybe I'm a little confused. Is there any reason why you aren't just using category.php?

  • Hi, Thanks, I was trying the wrong approach.

    By the way, I have created the category.php using the following code so the category-slug is displayed inside the string based on the current category:

    <?php
        $cat = get_query_var('cat');
        $yourcat = get_category ($cat);
      ?>
    
    <?php query_posts('category_name='. $yourcat->slug); ?>
    
    ...