Show Only Parent Categories on WordPress Archives

December 2nd, 2010 | Posted in Wordpress
0

Show only the parent category for the archive the user is currently viewing. It will not show any child categories.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
// Get the current category
foreach((get_the_category()) as $category) 
{ 
	$current_cat_id = $category->cat_ID; 
	break;
}
 
// Set the category to only the category selected
$args = array(
	'category__in' => array($current_cat_id),
	'orderby' => 'date',
	'order' => 'DESC'
);
 
$posts = new WP_Query();
$posts->query($args);
 
if ($posts->have_posts())
{
	while ($posts->have_posts())
	{
		$posts->the_post();
		?> 
		<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
 
		<p>
			<?php the_content(); ?>
		</p>
		<?php 
	}	
}
 
// Reset global query
wp_reset_query();
?>

Other Interesting Posts

Leave a Reply