コード例 #1
0
/**
 * Get first attached image source with set size.
 * 
 * @param int $post_id
 * @param string $size
 * @param boolean $show_hidden
 * @return string 
 */
function ale_get_first_attached_image_src($post_id, $size = 'thumbnail', $show_hidden = true)
{
    if (!$post_id) {
        $post_id = get_the_ID();
    }
    $image = ale_get_first_attached_image($post_id, $show_hidden);
    if ($image) {
        $image_img = wp_get_attachment_image_src($image->ID, $size);
        return $image_img[0];
    } else {
        return '';
    }
}
コード例 #2
0
ファイル: front.php プロジェクト: BMNTeam/myFirstThemeToTf
function ale_get_og_meta_image()
{
    global $post;
    $thumbdone = false;
    $og_image = '';
    //Featured image
    if (function_exists('get_post_thumbnail_id')) {
        $attachment_id = get_post_thumbnail_id($post->ID);
        if ($attachment_id) {
            $og_image = wp_get_attachment_url($attachment_id, false);
            $thumbdone = true;
        }
    }
    //From post/page content
    if (!$thumbdone) {
        $image = ale_parse_first_image($post->post_content);
        if ($image) {
            preg_match('~src="([^"]+)"~si', $image, $matches);
            if (isset($matches[1])) {
                $image = $matches[1];
                $pos = strpos($image, site_url());
                if ($pos === false) {
                    if (stristr($image, 'http://') || stristr($image, 'https://')) {
                        $og_image = $image;
                    } else {
                        $og_image = site_url() . $image;
                    }
                } else {
                    $og_image = $image;
                }
                $thumbdone = true;
            }
        }
    }
    //From media gallery
    if (!$thumbdone) {
        $image = ale_get_first_attached_image($post->ID);
        if ($image) {
            $og_image = wp_get_attachment_url($image->ID, false);
            $thumbdone = true;
        }
    }
    return $og_image;
}