admin管理员组文章数量:1336419
I'm trying to manually fill the Open Graph tags and I'm having some troubles in setting the content for the og:image
tag.
In a single post page, I set it this way:
<?php
$thumbnailSrc = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
$image = esc_attr($thumbnailSrc[0]);
?>
<meta property="og:image" content="<?php echo $image ?>">
The result is this:
<meta property="og:image" content="/wp-content/uploads/image.jpg">
On the Open Graph debugger I then get this error:
Object at URL '' of type 'article' is invalid because the given value '/wp-content/uploads/image.jpg' for property 'og:image:url' could not be parsed as type 'url'.
How can I get the attachment so that the url is: .jpg ?
I'm trying to manually fill the Open Graph tags and I'm having some troubles in setting the content for the og:image
tag.
In a single post page, I set it this way:
<?php
$thumbnailSrc = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
$image = esc_attr($thumbnailSrc[0]);
?>
<meta property="og:image" content="<?php echo $image ?>">
The result is this:
<meta property="og:image" content="/wp-content/uploads/image.jpg">
On the Open Graph debugger I then get this error:
Object at URL 'http://website' of type 'article' is invalid because the given value '/wp-content/uploads/image.jpg' for property 'og:image:url' could not be parsed as type 'url'.
How can I get the attachment so that the url is: http://website/wp-content/uploads/image.jpg ?
Share Improve this question asked Jun 20, 2017 at 9:17 CarloCarlo 1695 silver badges14 bronze badges3 Answers
Reset to default 1esc_attr()
might not be necessary on the url
retrieved by wp_get_attachment_image_src
.
I have referred to a code example from the WordPress Codex page on wp_get_attachment_image_src and adapted the following code that works for me.
global $post;
$thumbnailSrc = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
if ( $thumbnailSrc ) :
echo '<meta property="og:image" content="'.$thumbnailSrc[0].'">';
endif;
Edit: Since you are using the $post object outside of the WordPress loop you will need to declare global $post;
before you use $post->ID
. I have added it to the code sample above.
You could use has_post_thumbnail() and get_the_post_thumbnail_url() to get an absolute url for the posts feature image.
According to the Codex has_post_thumbnail()
will check if the post has an image attached and get_the_post_thumbnail_url()
will return the post thumbnail URL.
I have tested the code below:
global $post;
if ( has_post_thumbnail($post->ID) ) {
echo '<meta property="og:image" content="'.get_the_post_thumbnail_url($post->ID).'">';
}
As mentioned in a previous comment, you are using the $post object before the WordPress loop starts and will need to declare global $post;
before you can use $post->ID
.
To access the current post object outside The Loop, you need to declare the $post
variable globally.
<?php global $post; ?>
<meta property="og:image"
content="<?php echo wp_get_attachment_url(get_post_thumbnail_id(($post->ID)); ?>">
本文标签: imagesHow to get full absolute url for post attachment
版权声明:本文标题:images - How to get full absolute url for post attachment? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742379454a2463809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论