0

I am creating a category template that uses WP_query to get posts from my custom post type...

<?php 
$query = new WP_Query( array( 'post_type' => array( 'mycpt' ), 'posts_per_page' => 5 ) );
?>

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

<?php the_title(); ?>

<?php endwhile; ?>

How do i get the category id for the current category type? I can get the category title with...

<?php single_cat_title( '', true ); ?>

But how do I get the ID?

1

On a category archive, the "queried object" will have what you need.

$cat = get_queried_object();
var_dump($cat->cat_ID);

You can access it by means of get_query_var():

$cat = get_query_var('cat');
var_dump($cat);

You can also pull is straight from the $wp_query global:

var_dump($wp_query->query_vars['cat']);

All of these assume that you are actually on a proper category archive page and that nothing has clobbered or otherwise corrupted the main query.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.