Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi undergroundnetwork

    Normally only posts with a post thumbnail (featured image) are found as related posts. Did you use filters to display posts without a post thumbnail?

    Would you like the fallback image be one you’ve uploaded in the media library or one you’ve uploaded to a directory in your (child) theme?

    Thread Starter undergroundnetwork

    (@undergroundnetwork)

    Whoops.. didn’t realize I had posted the message! I wasn’t done writing it! Sorry about that!

    I’ve used some of WordPress’s code to implement where it grabs the first attached image and makes it a fallback:

    /***************************************************
    ::attached image
    ::https://codex.www.ads-software.com/Function_Reference/get_children#Examples
    :: Put - if ( ! has_post_thumbnail() { echo_first_image($post->ID); } in the template.
    ***************************************************/
    
    function echo_first_image( $postID ) {
    	$args = array(
    		'numberposts' => 1,
    		'order' => 'ASC',
    		'post_mime_type' => 'image',
    		'post_parent' => $postID,
    		'post_status' => null,
    		'post_type' => 'attachment',
    	);
    
    	$attachments = get_children( $args );
    
    	if ( $attachments ) {
    		foreach ( $attachments as $attachment ) {
    			$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' )  ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );
    
    			echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="current">';
    		}
    	}
    }

    I was just seeing if there was a way I could implement it into your code.
    I’m also in the process of adding in a default fallback image if there is not even an attached image.

    That goes something like:

    if ( ! $attachments ) {
    echo '<img src="' . wp_get_attachment_thumb_url( some-defult-image-id-here ) . '" class="current">';
    }

    I was going to try to implement the code and put it here for you, but I’m not that good of a coder.

    Thanks!

    Plugin Author keesiemeijer

    (@keesiemeijer)

    First of, if you use the format ‘thumbnails’ there should be no related posts found without a thumbnail.

    1. Do you want to use the first attached image instead of the post thumbnail (for posts with a post thumbnail)?
    2. Or do you want to search for related posts regardless if they have a post thumbnail, show the fallback if the post doesn’t have a (first) image attached?

    Thread Starter undergroundnetwork

    (@undergroundnetwork)

    I’d like the plugin to grab ALL related posts, by using your code:

    $args = array(
           'post_thumbnail' => 0,
        );

    1 – If there IS a featured image, it will show it like it already does.

    2 – If there is no featured image, then it should grab the first attached image if there is one. Which my code above does.

    3 – If there is no attached image, then it will grab a default (fallback) image that I have set.

    I’ll try to write the code now and post it here if you want to add it to your plugin.

    Thanks!

    Charles

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Aha, that makes sense ??

    Are you displaying the related posts yourself, with the km_rpbt_related_posts_by_taxonomy() function? Or are you using the widget or/and shortcode?

    I guess you use the function. I appreciate if you post the code you have now. My time today is limited. I have some time tomorrow to help you with this.

    Thread Starter undergroundnetwork

    (@undergroundnetwork)

    I’m using the echo shortcode approach..

    <?php echo do_shortcode( '[related_posts_by_tax]' ); ?>

    I’m using the code above for my lists of posts, where i have:

    if ( has_post_thumbnail() {
    the_post_thumbnail()
    } else {
    echo_first_image($post->ID);
    }

    I don’t have the fallback image as part of the code yet.

    Charles

    Thread Starter undergroundnetwork

    (@undergroundnetwork)

    OK, I’m using the functions.. and it won’t show related posts even if I tell it to show posts without thumbnails:

    <?php // first get the related post objects with the km_rpbt_related_posts_by_taxonomy() function.
    
    // check if the function already exists
    if ( function_exists( 'km_rpbt_related_posts_by_taxonomy' ) ) {
    
        $args = array(
            'posts_per_page' => 3,
            'post_thumbnail' => 0,
        );
    
        $taxonomies = array( 'category', 'post_tag' );
        $post_id =  $post->ID; // post id available inside the loop.
    
        $related_posts = km_rpbt_related_posts_by_taxonomy( $post_id , $taxonomies, $args );
    
        // check if related post were found
        if ( $related_posts ) {
    
            $args = array(
                'columns'    => 3,
                'size'       => 'thumbnail', // Default "thumbnail", "medium", "large", "full"
            );
    
            // display the related post thumbnail gallery
            echo km_rpbt_related_posts_by_taxonomy_gallery( $args, $related_posts );
        }
    } ?>

    It just shows 2 posts and the 3rd is blank. Is there something I am doing wrong?

    Plugin Author keesiemeijer

    (@keesiemeijer)

    That’s because the km_rpbt_related_posts_by_taxonomy_gallery() function function doesn’t show an image for related posts that have no post thumbnail. The good thing is you can add a filter to the gallery images.

    Try it with this in your theme’s functions.php

    // Filter the post thumbnail image link
    add_filter( 'related_posts_by_taxonomy_post_thumbnail_link', 'rpbt_related_post_image', 10, 4 );
    
    function rpbt_related_post_image( $image, $attr, $related, $args ) {
    
    	if ( !empty( $image ) ) {
    		// Post thumbnail found.
    		return $image;
    	}
    
    	$image_args = array(
    		'numberposts' => 1,
    		'order' => 'ASC',
    		'post_mime_type' => 'image',
    		'post_parent' => $related->ID,
    		'post_status' => null,
    		'post_type' => 'attachment',
    	);
    
    	$attachments = get_children( $image_args );
    
    	if ( !empty( $attachments ) ) {
    		// Attached images found
    		$attachments = array_values( $attachments );
    		$id = (int) $attachments[0]->ID;
    
    		// first attachment image
    		$image = wp_get_attachment_image( $id , $args['size'], false, $attr['describedby'] );
    	} else {
    		// fallback image
    		// Change 1790 to the id you want as fallback image
    		$image = wp_get_attachment_image( 1790, $args['size'], false, $attr['describedby'] );
    	}
    
    	$image = $image ? "<a href='{$attr['permalink']}' title='{$attr['title']}'>{$image}</a>" : '';
    
    	return $image;
    }

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi undergroundnetwork

    With some extra code you can use the fallback images in the widget and shortcode as well. Read about it here

    Thread Starter undergroundnetwork

    (@undergroundnetwork)

    Fantastic! That is so great that you did that! Will try it out when I get back to my conputer.

    You are by far one of the most helpful plugin developers there is which is why you fully deserve 5 stars and have been getting them.

    And your plugin is so lightweight, and just works so well.

    Thank you for everything!

    Charles

    Plugin Author keesiemeijer

    (@keesiemeijer)

    You’re welcome ?? I hope it solves your issue. Let me know here.

    Thanks for the great review ??

    Thread Starter undergroundnetwork

    (@undergroundnetwork)

    Just put the code for the shortcode version and it works perfectly!

    Oh.. I just noticed a couple typos on your website, the beginning php tags on this page do not have the question marks, I think you need them unless I am mistaken.

    <php km_rpbt_related_posts_by_taxonomy( $post_id, $taxonomies, $args  ); ?>
    
    and
    
    <php km_rpbt_related_posts_by_taxonomy_gallery( $args, $related_posts  ); ?>

    Just wanted to point them out just in case.

    Thanks!

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Great to hear.

    And thanks for the typos. I’ve updated the examples.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Fallback Thumbnail Image?’ is closed to new replies.