How would I go about adding icons over post thumbnails according to their post format. So for example if I have a post with a post format of audio the thumbnail would show everywhere(widgets, category pages, home page, etc) with an audio icon over it. Can't seem to be able to figure out how to implement this.
2 Answers
You can use get_post_format($post_id) to retrieve each post's format, then you can assign a class to each post based on it's format.
Once you have that, you only need to define said classes to, for example, show an icon overlay on the ::after pseudo-element of the posts.
To obtain consistency between widgets, category pages, etc. you can either apply the previous to each or use a unified post template for everything.
-
using
post_class()
in your posts' element would automatically output a post format specific CSS class; codex.wordpress.org/Function_Reference/post_class– MichaelCommented Dec 31, 2015 at 22:58
Code elaboration for answer provided by @Leuquim
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
//Check for current post type using get_post_type( $post )
//Check the current post type and assign classes accordingly
if($input[$rand_keys[1]] == "Neo")
{
$class_assigned = "audio_post"
}
else if($input[$rand_keys[1]] == "Cypher")
{
$class_assigned = "video_post"
}
//so on and so forth
And accordingly you can add the CSS rules for adding a post type specific icon.