• Resolved KranzKrone

    (@kranzkrone)


    I have another Problem with Koji you might can help me with…
    (For that i looked twice, if a Solution is already there)

    Like described in the Titel of this Thread, i wanna have the “First Image in a Post as the Thumbnail” but without double it as in the Single-Post. Tried some Stuff already but with little to no help…

    For some previous Themes (Garfunkel with a Child-Theme) i used the following Code-Snippet but now with Koji it didn’t work that good anymore.
    https://www.wptutor.io/wordpress/snippets/first-image-thumbnail

    When i use this Code-Snippet with Koji i get the Image in the Single-Post doubled and that’s not what i want. Maybe you as a Developer can fix this Issue because you know so much more about Coding… ??

    If you can solve this Problem of mine, there is another Question following. Would it be possible to deactivate this kind of “new Behavior” for a Post manually?

    • This topic was modified 5 years, 11 months ago by KranzKrone.
    • This topic was modified 5 years, 11 months ago by KranzKrone.
    • This topic was modified 5 years, 11 months ago by KranzKrone.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Theme Author Anders Norén

    (@anlino)

    Hi @kranzkrone,

    Try replacing the code in the article with the following:

    add_filter( 'get_post_metadata', function( $value, $object_id, $meta_key, $single ) {
    	
    	if ( $meta_key !== '_thumbnail_id' || $value ) {
    		return $value;
    	}
    
    	preg_match( '~<img[^>]+wp-image-(\\d+)~', get_post_field( 'post_content', $object_id ), $matches );
    	if ( $matches && ! is_single() ) {
    		return $matches[1];
    	}
    
    	return $value;
    
    }, 10, 4 );

    This version amends the conditional with a ! is_single() function call, which makes sure that it’s only triggered on archive pages.

    — Anders

    Thread Starter KranzKrone

    (@kranzkrone)

    Hey @anlino,

    at first Glance your Code-Snippet works just fine but within the ?Single-View“ of an Article in the Post it doesn’t work within/for the ?Related Posts“ at the Bottom.

    There at the Bottom for the ?Related Posts“, it still shows this BLANK-IMAGE kinda Placeholder. How can i fix this or can i even fix it?

    Thank you for the Help so far. ??

    Hey @anlino & @kranzkrone,

    Is there anything we can do on this? I am trying to edit koji_get_fallback_image_url() but it’s not working.

    – Nithin

    Theme Author Anders Norén

    (@anlino)

    Hi @kranzkrone,

    Sorry for the late reply – I couldn’t find an obvious solution for getting this to work using the same filter structure. I did find a way to accomplish it by plugging the koji_get_fallback_image_url() function in your child theme as $nithinnn suggested, though.

    Try the following code:

    function koji_get_fallback_image_url() {
    
    	$disable_fallback_image = get_theme_mod( 'koji_disable_fallback_image' );
    
    	if ( $disable_fallback_image ) {
    		return '';
    	}
    
    	global $post;
    
    	preg_match( '~<img[^>]+wp-image-(\\d+)~', get_post_field( 'post_content', $post->ID ), $matches );
    
    	if ( $matches ) {
    		$image_url = wp_get_attachment_image_url( $matches[1], koji_get_preview_image_size() );
    		return $image_url;
    	}
    
    	$fallback_image_id = get_theme_mod( 'koji_fallback_image' );
    
    	if ( $fallback_image_id ) {
    		$fallback_image = wp_get_attachment_image_src( $fallback_image_id, 'full' );
    	}
    
    	$fallback_image_url = isset( $fallback_image ) ? esc_url( $fallback_image[0] ) : get_template_directory_uri() . '/assets/images/default-fallback-image.png';
    
    	return $fallback_image_url;
    
    }

    The koji_get_fallback_image_url() function is only used in previews, so it should work for your purposes. Note that this won’t tak effect if the fallback image setting in the Customizer is disabled.

    — Anders

    Thank you @anlino,

    I was thinking to sit on it today and fix it, Your solution works.

    Thanks a lot.

    – Nithin

    Thread Starter KranzKrone

    (@kranzkrone)

    THANK you a lot @anlino for solving this Problem of mine. ??

    IT works just fine.

    I like, that you Answered after all. There are lots of Developers out there, that don’t even answer once but you Mr. are great!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘First Image in a Post as the Thumbnail, without double in Single?’ is closed to new replies.