Grow your CSS skills. Land your dream job.

How to Get Category-ID or Category Slug Inside the query_post String? (WordPress)

  • # February 20, 2013 at 3:32 pm

    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?

    # February 20, 2013 at 3:53 pm

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

    # February 21, 2013 at 7:31 am

    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); ?>

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".