You can make use of the conditional tag has_catgeory
. You can do something like this
if(has_category( 'cars' ) || has_category( 'uncategorized' )) {
//display cars image
}elseif(has_category( 'banana' )) {
//display banana image
}
From your comments, here is your code
<?php if(has_category( 'Update' ) || has_category( 'uncategorized' )) { ?>
<img alt="Hello!" src="https://wordpress.stackexchange.com/questions/161368/<?php bloginfo("template_directory'); ?>/img/loud.png">
<?php } elseif(has_category( 'Event' )) { ?>
<img alt="Hello!" src="https://wordpress.stackexchange.com/questions/161368/<?php bloginfo("template_directory'); ?>/img/event.png">
<?php } ?>
EDIT
The conditional tag in_category
can also be used for this purpose
EDIT 2
For the second part of your question, check out this post by @JohannesPille on SO. Here is the post reposted.
PLEASE NOTE: I did upvote the answer on SO. If anyone see this as unfit, please rollback this edit 🙂
The action create_category
runs when a new category is created.
You want your category creation function to run when the theme is activated. The relevant action is after_setup_theme
.
Drop this in your theme’s functions.php and you should be good to go:
function create_my_cat () {
if (file_exists (ABSPATH.'/wp-admin/includes/taxonomy.php')) {
require_once (ABSPATH.'/wp-admin/includes/taxonomy.php');
if ( ! get_cat_ID( 'Testimonials' ) ) {
wp_create_category( 'Testimonials' );
}
}
}
add_action ( 'after_setup_theme', 'create_my_cat' );