WordPress: Displaying Categories Differently in The Loop
WordPress has become increasingly popular around the developers community as a great Content Management System. We’ve used WordPress for countless projects and every time we build with WP we learn some new tricks and tips.
Once of our most recent projects required categories to display different on the homepage. Essentially the client had 2 categories, ‘News’ and “Blog Posts” and he wanted the news section to display in full–the entire post, without a link to the actual post. For the regular blog posts, he wanted the the title and excerpt and of course, a link to the full post.
The solution ended up being pretty easy.
$category = get_the_category();
$category = $category[0]->cat_name;
Using get_the_category allowed me to store the category in a variable without automatically echoing to the page. I then used the variable to define the markup needed for each category. My final code is as follows:
cat_name;
if ($category == "News") {
//if news, display all content
the_content();
} else {
//if post, display excerpt
the_excerpt();
}
?>





socialize