Any info on how to solve this? I think it is very much needed as a fallback solution if a taxonomy doesn’t have an image or Post to show!
thanks in advance,
kind regards
First you will need to upgrade to version 0.7, Then you can add the following code to your theme’s functions.php file:
function mytheme_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if ( ! empty( $html ) ) {
return $html;
}
$categories = (array) apply_filters( 'taxonomy-images-get-the-terms', array(), array() );
if ( empty( $categories ) ) {
return $html;
}
$image = '';
if ( isset( $categories[0]->image_id ) ) {
$image = wp_get_attachment_image( $categories[0]->image_id, $size, false, $attr );
}
if ( ! empty( $image ) ) {
return $image;
}
return $html;
}
add_filter( 'post_thumbnail_html', 'mytheme_post_thumbnail_fallback', 10, 5 );
This will hook into the get_the_post_thumbnail() function. If there is not post thumbnail set, it will use the “Taxonomy Image” from the category associated with the post. In the event that the post is in more than one category, this function will only return the first category’s image. I believe that categories will be sorted in alphabetical order.
You can then add the following code inside the loop to display images:
<?php
$featured_image = get_the_post_thumbnail();
if ( ! empty( $featured_image ) ) {
print $featured_image;
}
?>
Hi Michael
Thanks for the prompt update, but maybe we misunderstood each other, or it wasn’t to clear from my side.
I would just need a if statement weather there is an image attached to the category/taxonomy or not?
I’m using the following code in my category.php
print apply_filters( 'taxonomy-images-queried-term-image', '', array( 'attr' => array('class' => 'headerimage'),'image_size' => 'header' ) );
hope that makes it more understandable!
Thanks again and kind regards
My bad, Got to that one pretty early this morning. Here’s how you do what you asked for:
/*
* STEP #1
* Assign the output of the filter to a variable.
*/
$image = apply_filters( 'taxonomy-images-queried-term-image', '', array(
'attr' => array( 'class' => 'headerimage' ),
'image_size' => 'header' )
);
/*
* STEP #2
* Now we will check the value of the $image variable.
* if it is anything other than "empty" we will print
* the value.
*/
if ( ! empty( $image ) ) {
print $image;
}
Hey no problem,
thanks a lot for taking time to help out! Worked as intended!
Best regards