Setup a Query to show Archive by Category | WordPress

Here is my code for my archive widget. I want to show only 1 category of posts in my archive. How is that possible? Thanks in advance.

// If there’s a post type archive
if(is_post_type_archive()) {
$post_type = get_query_var(‘post_type’);
if(is_array($post_type))
$post_type = reset($post_type);
$post_type_object = get_post_type_object($post_type);
if(! $post_type_object->has_archive)
$title = post_type_archive_title(”, false);
}

// If there’s a category or tag
if(is_category() || is_tag()) {
$title = single_term_title(”, false);
}

// If there’s a taxonomy
if(is_tax()) {
$term = get_queried_object();
if($term) {
$tax = get_taxonomy($term->taxonomy);
$title = single_term_title(”, false);
}
}

// If there’s an author
if(is_author()) {
$author = get_queried_object();
if($author)
$title = $author->display_name;
}

// Post type archives with has_archive should override terms.
if(is_post_type_archive() && $post_type_object->has_archive)
$title = post_type_archive_title(”, false);

// If there’s a month
if(is_archive() && !empty($m)) {
$my_year = substr($m, 0, 4);
$my_month = $wp_locale->get_month(substr($m, 4, 2));
$my_day = intval(substr($m, 6, 2));
$title = $my_year . ($my_month ? $t_sep . $my_month : ”) . ($my_day ? $t_sep . $my_day : ”);
}

// If there’s a year
if(is_archive() && !empty($year)) {
$title = $year;
if(!empty($monthnum))
$title .= $t_sep . $wp_locale->get_month($monthnum);
if(!empty($day))
$title .= $t_sep . zeroise($day, 2);
}

Read more here:: Setup a Query to show Archive by Category | WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *