在 WordPress 页面上动态插入 og:image
Dynamically inserting og:image on a WordPress page
我正在使用以下功能,并且在我确定有特色图片集的页面上使用 FB 的调试器:
function fb_opengraph() {
global $post;
if(is_page()) {
if(has_post_thumbnail($post->ID)) {
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
} else {
$img_src = get_stylesheet_directory_uri() . '/img/opengraph_image.jpg';
}
if($excerpt = $post->post_excerpt) {
$excerpt = strip_tags($post->post_excerpt);
$excerpt = str_replace("", "'", $excerpt);
} else {
$excerpt = get_bloginfo('description');
}
?>
<meta property="og:title" content="<?php echo the_title(); ?>"/>
<meta property="og:description" content="<?php the_content(); ?>"/>
<meta property="og:type" content="article"/>
<meta property="og:url" content="<?php echo the_permalink(); ?>"/>
<meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/>
<meta property="og:image" content="<?php echo $img_src; ?>"/>
<?php
} else {
return;
}
}
add_action('wp_head', 'fb_opengraph', 5);
当前发生的情况是 $img_src 返回值 "Array",而不是该页面特色图片的 URL。我不确定这个 "Array" 值是从哪里来的,但更重要的是我试图在没有运气的情况下拉入特色图片 URL。
有什么想法吗?谢谢!
https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/
这应该给你一个数组
wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
为了得到 url 你需要得到合适的数组元素
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium')['url'];
试一试
wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
它将return一个数组。该数组的第一个元素将包含图像 URL.
所以你需要这样提到这个数组的第一个索引:$img_src[0]
完整行将是:
<meta property="og:image" content="<?php echo $img_src[0]; ?>"/>
我正在使用以下功能,并且在我确定有特色图片集的页面上使用 FB 的调试器:
function fb_opengraph() {
global $post;
if(is_page()) {
if(has_post_thumbnail($post->ID)) {
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
} else {
$img_src = get_stylesheet_directory_uri() . '/img/opengraph_image.jpg';
}
if($excerpt = $post->post_excerpt) {
$excerpt = strip_tags($post->post_excerpt);
$excerpt = str_replace("", "'", $excerpt);
} else {
$excerpt = get_bloginfo('description');
}
?>
<meta property="og:title" content="<?php echo the_title(); ?>"/>
<meta property="og:description" content="<?php the_content(); ?>"/>
<meta property="og:type" content="article"/>
<meta property="og:url" content="<?php echo the_permalink(); ?>"/>
<meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/>
<meta property="og:image" content="<?php echo $img_src; ?>"/>
<?php
} else {
return;
}
}
add_action('wp_head', 'fb_opengraph', 5);
当前发生的情况是 $img_src 返回值 "Array",而不是该页面特色图片的 URL。我不确定这个 "Array" 值是从哪里来的,但更重要的是我试图在没有运气的情况下拉入特色图片 URL。
有什么想法吗?谢谢!
https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/
这应该给你一个数组
wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
为了得到 url 你需要得到合适的数组元素
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium')['url'];
试一试
wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
它将return一个数组。该数组的第一个元素将包含图像 URL.
所以你需要这样提到这个数组的第一个索引:$img_src[0]
完整行将是:
<meta property="og:image" content="<?php echo $img_src[0]; ?>"/>