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?
lien caché
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....
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
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
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?
lien caché
Thanks,
Shane
Thanks i will try it and tell you if it's ok.
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>lien caché</u></em>" xmlns:fb="<em><u>lien caché</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.....
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 );
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....
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
Hi Shane,
I'm back again, and i found an alternative solution.
Thanks 😉
My issue is resolved now. Thank you!