og:image 如何在这 3 个选项之间进行选择
How to choose between this 3 options for og:image
我试着编写这样的代码:
- 如果 post 没有缩略图,它会选择默认图像。
- 如果没有 ACF 图片则选择默认缩略图
- 如果有 ACF 图像,则使用它。
这是我的 og:image 脸书。
function insert_image_src_rel_in_head()
{
global $post;
if (!is_singular())
return;
if (!has_post_thumbnail($post->ID))
{
$default_image = "http://madmax.quebec/neufsvies/wp-content/uploads/2016/09/madmax.quebec_qaygerxdm814v-750x374.jpg";
echo '<meta property="og:image" content="' . $default_image . '"/>';
}
elseif (!empty($temp_acfImage))
{
$temp_acfImage = wp_get_attachment_image_src(get_field('image_og'), 'facebook-thumbnail');
echo '<meta property="og:image" content="' . esc_attr($temp_acfImage[0]) . '"/>';
}
else
{
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'facebook-thumbnail');
echo '<meta property="og:image" content="' . esc_attr($thumbnail_src[0]) . '"/>';
}
echo "";
}
add_action('wp_head', 'insert_image_src_rel_in_head', 1);
尝试使用此代码:
<?php
function insert_image_src_rel_in_head()
{
global $post;
$img_src = '';
$default_image = "http://madmax.quebec/neufsvies/wp-content/uploads/2016/09/madmax.quebec_qaygerxdm814v-750x374.jpg"; //default image src
if (!is_singular())
return;
$acfImage = get_field('image_og', $post->ID);
//if post has ACF 'image_og'
if (!empty($acfImage))
{
$img_src = wp_get_attachment_image_src($acfImage, 'facebook-thumbnail');
}
//if post does't has ACF 'image_og' but has thumbnail
else if (has_post_thumbnail($post->ID))
{
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'facebook-thumbnail')[0];
}
//if post does't has thumbnail and also does't has ACF 'image_og' then set a default image
else
{
$img_src = $default_image;
}
?>
<meta property="og:image" content="<?php echo $img_src; ?>"/>
<?php
}
add_action('wp_head', 'insert_image_src_rel_in_head', 5);
?>
wp_get_attachment_url
it returns URI to uploaded attachment or
"false" on failure; whereas wp_get_attachment_image_src
it
returns an array contains four values: the URL of the attachment image
src, the width of the image file, the height of the image file, and a
boolean. And wp_get_attachment_url
only takes $attachment_id
as param and > wp_get_attachment_image_src
accept $attachment_id
, $size
.
代码未经测试,但应该可以工作。
希望这对您有所帮助!
我试着编写这样的代码:
- 如果 post 没有缩略图,它会选择默认图像。
- 如果没有 ACF 图片则选择默认缩略图
- 如果有 ACF 图像,则使用它。
这是我的 og:image 脸书。
function insert_image_src_rel_in_head()
{
global $post;
if (!is_singular())
return;
if (!has_post_thumbnail($post->ID))
{
$default_image = "http://madmax.quebec/neufsvies/wp-content/uploads/2016/09/madmax.quebec_qaygerxdm814v-750x374.jpg";
echo '<meta property="og:image" content="' . $default_image . '"/>';
}
elseif (!empty($temp_acfImage))
{
$temp_acfImage = wp_get_attachment_image_src(get_field('image_og'), 'facebook-thumbnail');
echo '<meta property="og:image" content="' . esc_attr($temp_acfImage[0]) . '"/>';
}
else
{
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'facebook-thumbnail');
echo '<meta property="og:image" content="' . esc_attr($thumbnail_src[0]) . '"/>';
}
echo "";
}
add_action('wp_head', 'insert_image_src_rel_in_head', 1);
尝试使用此代码:
<?php
function insert_image_src_rel_in_head()
{
global $post;
$img_src = '';
$default_image = "http://madmax.quebec/neufsvies/wp-content/uploads/2016/09/madmax.quebec_qaygerxdm814v-750x374.jpg"; //default image src
if (!is_singular())
return;
$acfImage = get_field('image_og', $post->ID);
//if post has ACF 'image_og'
if (!empty($acfImage))
{
$img_src = wp_get_attachment_image_src($acfImage, 'facebook-thumbnail');
}
//if post does't has ACF 'image_og' but has thumbnail
else if (has_post_thumbnail($post->ID))
{
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'facebook-thumbnail')[0];
}
//if post does't has thumbnail and also does't has ACF 'image_og' then set a default image
else
{
$img_src = $default_image;
}
?>
<meta property="og:image" content="<?php echo $img_src; ?>"/>
<?php
}
add_action('wp_head', 'insert_image_src_rel_in_head', 5);
?>
wp_get_attachment_url
it returns URI to uploaded attachment or "false" on failure; whereaswp_get_attachment_image_src
it returns an array contains four values: the URL of the attachment image src, the width of the image file, the height of the image file, and a boolean. Andwp_get_attachment_url
only takes$attachment_id
as param and >wp_get_attachment_image_src
accept$attachment_id
,$size
.
代码未经测试,但应该可以工作。
希望这对您有所帮助!