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
Add a comment  | 

2 Answers 2

Reset to default 1

Use 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