reference the current category being used in the category.php page

I have this code in my category.php file

            <?php query_posts( array('post_type'=>'featured' )); 

            $featured = new WP_Query( array('posts_per_page'=>1, 'tax_query'=> array(
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'fever'
            )
        )));

                while ( $featured->have_posts()) : $featured->the_post();
                unisphere_get_post_image('normal-slider');?>
                <?php endwhile; wp_reset_query(); ?>

This code works, but what I need is where it says ‘terms’ => ‘fever’ to instead reference whatever the current category being used in the category.php page would be. I need it to work this way so that my featured image changes out with the current category that being used by the page category.php at that moment.

// Got the answer I needed. Here’s a copy of the working code.

            <?php 
            $term = get_queried_object();
            query_posts( array('post_type'=>'featured' )); 

            $featured = new WP_Query( array('posts_per_page'=>1, 'tax_query'=> array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => $term
                        )
                    ) 
                ));

            while ( $featured->have_posts()) : $featured->the_post();
            unisphere_get_post_image('normal-slider'); ?>
            <?php endwhile; wp_reset_query(); ?>

1 Answer
1

Try using get_queried_object():

$term = get_queried_object();
echo $term->name;

If you get nothing there, try this:

print_r($wp_query);

Everything you need is in the $wp_query global.

Hope this helps you.

Leave a Comment