Skip Navigation

[Resolved] Use en image custom field as image meta

This support ticket is created 2 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 2 voices.

Last updated by jeanL-2 2 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#2337155

Tell us what you are trying to do?
I want to use an image from a custom field as meta property

What is the link to your site?
hidden link

I saw: <meta property="og:url" content="<?php do_shortcode('[types field='affiliate-url' output='raw'][/types]'); ?>" /> but I don't know where to put it....

#2337167

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jean,

Thank you for getting in touch.

This code will need to be placed in you post's php template. Given that the post loop will be in the post body template.

An example of where to place it is in your post.php template for your theme.

If you're still not sure please let me know.

Thanks,
Shane

#2338323

Hi Shane,

I'm using elementor, and hello theme.

I found single.php, header.php and dynamic-header.php, i tried to put the code " <meta property="og:image" content="<?php echo types_render_field('image-a-mettre-a-la-une', array()) ?>" />" in all of theme without success...

Is there a way to make it with a php code in function.php ?

Jean

#2338979

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jean,

I was able to find a tutorial on this for you.

Can you try the following and let me know if it helps?
hidden link

Thanks,
Shane

#2342611

Thanks i will try it and tell you if it's ok.

#2342659

Hi i tried it with this code :

//Adding the Open Graph in the Language Attributes
function add_opengraph_doctype( $output ) {
        return $output . ' xmlns:og="<em><u>hidden link</u></em>" xmlns:fb="<em><u>hidden link</u></em>"';
    }
add_filter('language_attributes', 'add_opengraph_doctype');
 
//Lets add Open Graph Meta Info
 
function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) //if it is not a post or a page
        return;
        
    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
        $default_image="<?php echo types_render_field('image-a-mettre-a-la-une', array()) ?>"; //replace this with a default image on your server or an image in your media library
        echo '<meta property="og:image" content="' . $default_image . '"/>';
    }
    else{
        $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
        echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    }
    echo "
";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );

with no succes.....

#2343333

My issue is resolved now. Thank you!

Here is the code to put in functions.php


function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) //if it is not a post or a page
        return;
        
    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
        $default_image="<?php do_shortcode('[types field='your-custom-code' output='raw'][/types]'); ?>"; //replace this with a default image on your server or an image in your media library
        echo '<meta property="og:image" content="' . $default_image . '"/>';
    }
    else{
        $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
        echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    }
    echo "
";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );

#2343355

it worked but only once.... im so disapointed :-[ but how can i make it work, and another question can i make it work only for a specific post type....

#2344597

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jean,

Can you clarify what is meant by it only works once ?

Secondly you can use the function below to check the post type.
https://developer.wordpress.org/reference/functions/get_post_type/

An example usage would be.


if (get_post_type($post->ID ) == "post"){
//do something
}

Please let me know if this helps.
Thanks,
Shane

#2349311

Hi Shane,

I'm back again, and i found an alternative solution.

Thanks 😉

#2349313

My issue is resolved now. Thank you!