• Resolved Mrskt

    (@mrskt00)


    I Want to display featured image from bt_image_url custom field if featured image is empty.

    I am using this code but its not working:

    // Replace the default featured image with custom field image for blogger-template post type
    function bai_replace_featured_image_with_custom_field( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
        // Check if current post is of blogger-template post type
        if ( get_post_type( $post_id ) === 'blogger-template' ) {
            $image_url = rwmb_get_value( 'bt_image_url', $post->ID );
    		$alt_text = 'Check Out ' . rwmb_get_value( 'bt_name', $post->ID ) . ' Premium Blogger Template';
            // Check if the post has a featured image set already
            if ( empty( $html ) ) {
                $html = '<img src="' . esc_attr( $image_url ) . '" alt="' . esc_attr( $alt_text ) . '" />';
            }
        }
        return $html;
    }
    add_filter( 'post_thumbnail_html', 'bai_replace_featured_image_with_custom_field', 10, 5 );
Viewing 6 replies - 1 through 6 (of 6 total)
  • Eduard

    (@cteduard)

    Hello @mrskt00

    I have asked the developers for an opinion on this one. Should have a reply from them soon. ??

    Thanks!

    Theme Author creativethemeshq

    (@creativethemeshq)

    @mrskt00 Hello!

    First of all, the post_thumbnail_html filter works just fine with Blocksy, I just gave it a go. But, in order for it to work, the post needs to have a real featured image attached to it (that is, has_post_thumbnail() has to return true for that post). Otherwise, the filter won’t even be called.

    There are two options here (I would go with the 2nd one honestly):

    1. Re-write the blocksy_get_featured_image_output() function in a child theme from the inc/components/single/single-helpers.php. Re-write the function implementation to pull the image from your custom source. Note: this will be quite tricky to implement and you will be on your own to maintain this function.
    2. Follow this steps:
      • Rewrite your custom field to use attachment IDs (not URLs). I understand that this might or might not be possible, depending on where you’re taking your images from.
      • Implement a custom filter that we will give to you that will pull the image from that new source.

    If you are okay with strategy #2 — please let us know and we’ll implement the filter in the next Blocksy release. We will also give you a beta version with the implementation and also an example code snippet that you can use as a reference.

    If you absolutely must use URLs and not attachment IDs, please also let us know and we’ll think of something.

    Waiting for your reply so that we know how to move forward.

    Thanks and all the best!

    Thread Starter Mrskt

    (@mrskt00)

    Hello, I got this code which working:

    //featured image with custom field image for blogger-template post type
    function display_blogger_template_thumbnail( $html, $post_id ) {
        if ( get_post_type( $post_id ) == 'blogger-template' ) {
            if ( has_post_thumbnail( $post_id ) ) {
                return $html;
            } else {
                $bt_image_url = rwmb_get_value( 'bt_image_url', $post_id );
    			$alt_text = 'Check Out ' . rwmb_get_value( 'bt_name', $post->ID ) . ' Premium Blogger Template';
                if ( ! empty( $bt_image_url ) ) {
                    return '<img src="' . esc_url( $bt_image_url ) . '" alt="' . esc_attr( $alt_text ) . '">';
                }
            }
        }
        return $html;
    }
    add_filter( 'post_thumbnail_html', 'display_blogger_template_thumbnail', 10, 2 );

    But, my question is these images not come under <a class="ct-image-container boundless-image" element like other posts which has already featured image. And also width and height attributes is missing.

    Should I’ve to modify this code:

    return '<img src="' . esc_url( $bt_image_url ) . '" alt="' . esc_attr( $alt_text ) . '">';
    Theme Author creativethemeshq

    (@creativethemeshq)

    @mrskt00 yes, the issues that you mention are fixed in the #2 approach. Please let me know if you can switch your custom field to output attachment ID, not URLs.

    Also, can you please show us a screenshot of which featured images you are trying to replace. Is it on the single post page or on the archive pages?

    Thread Starter Mrskt

    (@mrskt00)

    @creativethemeshq I was using that code to place images through external URL by getting value from the custom field on archive pages whose posts don’t have featured image.

    I am facing one more problem, post_tag taxonomies are not showing in the archive page on the custom post type.

    I have already enabled the option to show the post meta of post tag taxonomy in my theme customiser and the post tag is showing in the singular post of blogger-template post type, but not in the loop of archive pages.

    Theme Author creativethemeshq

    (@creativethemeshq)

    @mrskt00 thanks for getting back to us!

    First of all, yes, that’s true — with Blocksy the post_thumbnail_html filter was getting executed only for post cards that actually have a featured image. For the ones that didn’t have one it wasn’t getting fired. Starting with the next update we will have that fixed.

    But, I have a better solution and a different filter for you, especially because you want to keep the same html structure in place (boundless class etc). Please remove all your code and put this one in place: gist.github.com

    Please note that for this snippet of code to work properly, you need Blocksy of version 1.8.80 (not live as of writing this post). A beta version can be downloaded from here: download

    This one should perform better for you.

    On your issue with post_tag, most likely there is a way to fix this issue but I’d strong advise you not to re-use taxonomies from one CPT to the other. A much better solution would be to just define a different blogger_template_tag taxonomy and use that one for your CPT. Mixing taxonomies would can lead to many other unexpected surprises and much of Blocksy’s CPT integration relies on the fact that you don’t mix them. We could try to find out a solution for you but, as time showed, it just isn’t worth it.

    Hope this helps!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Featured Image from Custom field value’ is closed to new replies.