Hello Support,
I have a problem I need to solve and already fond some code but can't get it together.
Problem:
We have a real estate site build with "Starter Theme" and your real restate example as a base.
All works fine only when we share a link to one of our properties to Facebook the wrong image show up on FB page. FB debug told us that no og:image is present and this is what I try to solve.
Browsing this forum and other sources I already found some code that should be able to input the og:image into our templates header but now I need to find out how to include OUR featured image (which is not the featured image of the post but a custom field set up with toolset.
The code I found is:
function insert_image_src_rel_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="<em><u>hidden link</u></em>"; //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_image_src_rel_in_head', 5 );
This above code checks if the post has the WP featured image and if not uses a default image to display as og:image. To this function I would like to add a second else that checks something like:
if post type = house or apt or plot than use the photo provided in the custom field "wpcf-property-photo".
I found this code to get the url of this photo (I hope it is correct);
$prop_image_url=get_metadata($meta_type="post", $item_id=$post->ID, $meta_key="wpcf-property-photo", $single=true);
Can you please help me get this two peace together?
Thanks,
Gregor
To check if the Post Type is of type "your_type" you can use this WordPress native Function:
get_post_type()
https://developer.wordpress.org/reference/functions/get_post_type/
To then get if the Custom Field "wpcf-property-photo" you can use the get_post_meta() function:
https://developer.wordpress.org/reference/functions/get_post_meta/ OR the Types native render_types_field():
https://toolset.com/documentation/customizing-sites-using-php/functions/
You can then use those functions to build an additional layer of if/else in your Custom PHP Snippet.
Hi Beda, Thanks a lot, muchisimas gracias, herzlichen Dank!
I'll try to get the pieces together but now have even more ingredients and still don't know how to cook a soup from it 🙂 Maybe stackoverflow is a better place to ask for help??!!
Thanks anyway!
Gregor
🙂
The problem is, it's Custom Code, and unless the types_render_meta(), not really related to Toolset.
I cannot provide Custom Code:
https://toolset.com/toolset-support-policy/
An example thou:
if ( get_post_type( '1' ) == 'slug_post_type' ) {
//if is true do something like get_post_meta()
get_post_meta( '1', 'wpcf-your_field_slug_here', true | false);
}
You can avoid to hardcode Post ID above by getting the current Post ID from the global $post, as your code already does:
You can populate a $variable with what you get above (so you can later call what you got from get_post_meta() by simply saying "echo $variable" as example)
Let me know if you need more help 😉
Hello Beda,
thanks a lot for your help!
Yes, I know about the support policy and totally understand why that is necessary.
But in my opinion, this question has todo with Toolset because the problem I face is due to the fact that I use Toolset "Starter Theme", Types and Views and not a so called "premium" real estate theme to build the website that probably already takes care of this missing functionality that I now need to implement manually.
I really appreciate your help and especially on a sunday, when I would not expect an answer at all.
Back to my problem:
Do I understand you correct that because I get the current Post ID from the global $post I can replace the get_post_type( '1' ) in your example code with get_post_type( $post->ID ) or better $post_id? What really confuses me is the more examples I look at the more different ways I see to accomplish the same(?) thing like:
$post->ID
$post_id
$item_id=$post->ID
Could you maybe check and point me to my error?
I use this function now but the output in the html is empty means <meta property="og:image" content=""/>
function insert_image_src_rel_in_head() {
global $post;
if ( !is_singular()) //if it is not a post or a page
return;
if (get_post_type( $post->ID ) == 'house' or 'apartment' or 'land' or 'office' ) {
$prop_image_url=get_post_meta( $post_id, 'wpcf-property-photo', true);
echo '<meta property="og:image" content="' . $prop_image_url . '"/>';
}
elseif (!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
$default_image="<em><u>hidden link</u></em>";
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_image_src_rel_in_head', 5 );
Thanks again and have a nice sunday!
Gregor
1. Do I understand you correct that because I get the current Post ID from the global $post I can replace the get_post_type( '1' ) in your example code with get_post_type( $post->ID ) or better $post_id? What really confuses me is the more examples I look at the more different ways I see to accomplish the same(?) thing like:
I know, it's confusing 🙂
- global $post will get an object of the Post (this holds many informations about the post)
https://codex.wordpress.org/Function_Reference/$post
- this is something that WordPress does for you, all you need is declare the global $post
- then, you have an object stored in this $post
- to access single Properties from an object, you do this:
$object->single_value_key
Translated to WordPress this becomes:
$post->ID
- $post_id is a variable, that you can populate with something (usually the POST ID)
- you could as well write $whatever or $item or $post_id_super_custom and populate it with something. This is called a variable:
hidden link
- you assign (populate) a variable with the "=" sign. So, this here:
$item=$post->ID
means, now $item holds the value of $post->ID
Does that make sense so far?
To answer, yes, you can replace '1' with as example $post->ID, or you create a variable first and then reuse it all over.
2. This is wrong:
get_post_meta( $post_id, 'wpcf-property-photo', true);
$post_id is never defined, you need to either say_
get_post_meta( $post->ID, 'wpcf-property-photo', true);
or
$post_id = $post->ID;
get_post_meta( $post_id, 'wpcf-property-photo', true);
3. If this still doesn't work, always check the "IF" conditions, that's where it fails most time.
Means, you remove the IF to see if the code works, then re-introduce it to see where the problem in the IF is.
I hope I have been able to clear some doubts!
Happy coding 🙂