admin管理员组文章数量:1125086
I have custom template and i need get og:image url in header. Class image location in DB wp_postmeta - meta_value i try with functions.php
function getOgImage()
{
global $wpdb;
$ogimage = $wpdb->get_results("SELECT DISTINCT post_id FROM wp_postmeta WHERE meta_value", OBJECT);
echo '<meta property="og:image" content="'.$ogimage.'"/>';
}
add_action('wp_head', 'getOgImage');
Im geting out:
<meta property="og:image" content="Array" />
I have custom template and i need get og:image url in header. Class image location in DB wp_postmeta - meta_value i try with functions.php
function getOgImage()
{
global $wpdb;
$ogimage = $wpdb->get_results("SELECT DISTINCT post_id FROM wp_postmeta WHERE meta_value", OBJECT);
echo '<meta property="og:image" content="'.$ogimage.'"/>';
}
add_action('wp_head', 'getOgImage');
Im geting out:
<meta property="og:image" content="Array" />
Share
Improve this question
asked May 16, 2020 at 13:19
RaitoRaito
112 bronze badges
2 Answers
Reset to default 1Use var_dump($ogimage);
to understand the contents of $ogimage
presumably it's returning an array of records from the database.
Generally you would retrieve post meta values with get_post_meta
WordPress Docs for get_post_meta()
Also there are plugins that sort out the og tags for you such as Yoast SEO
Yoast SEO plugin page on WordPress site
Problem solved!
Code:
function getOgImage()
{
global $wpdb;
$ogimage = get_post_meta(get_the_ID(), '_wcs_image', true);
if($ogimage)
echo '<meta property="og:image" content="'.$ogimage.'"/>';
}
add_action('wp_head', 'getOgImage');
Thanks to Bob!
本文标签: wpdbogimage functionsphp
版权声明:本文标题:wpdb - og:image functions.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736627413a1945703.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论