Php 在Woocommerce中创建动态Facebook OG
为了在每次我在Facebook上分享时获得正确的图像和链接,我在Wordpress/Woocommerce标题上做了如下操作:Php 在Woocommerce中创建动态Facebook OG,php,wordpress,facebook,facebook-graph-api,woocommerce,Php,Wordpress,Facebook,Facebook Graph Api,Woocommerce,为了在每次我在Facebook上分享时获得正确的图像和链接,我在Wordpress/Woocommerce标题上做了如下操作: <meta property="og:image" content="<?php the_post_thumbnail_url(); ?>" /> <meta property="og:title" content="<?php echo the_title(); ?> by Pixel Komando" /&g
<meta property="og:image" content="<?php the_post_thumbnail_url(); ?>" />
<meta property="og:title" content="<?php echo the_title(); ?> by Pixel Komando" />
<meta property="og:url" content="<?php echo get_permalink(); ?>" />
问题似乎出在您的og:url标记上。每一次我用它重新擦伤都是不同的。对我来说,这表明get\u permalink()
方法没有返回一致的结果
仅供参考,og:url
meta标记不是必需的,因此这里有一个简单的修复方法就是不使用它。只有当您有多个URL访问同一资源,并且您希望FB的爬虫程序(即URL)成为规范的爬虫程序时,您才真正需要它。因为它是一个归档页面,所以无论何时调用get\u permalink()
它都会选择最后一个或第一个产品URL,因此,我建议您从header.php
中删除代码,并在functions.php
中添加以下代码
function wh_doctype_opengraph($output) {
return $output . '
xmlns:og="http://opengraphprotocol.org/schema/"
xmlns:fb="http://www.facebook.com/2008/fbml"';
}
add_filter('language_attributes', 'wh_doctype_opengraph');
function wh_fb_opengraph()
{
global $post;
if (is_home() && is_front_page())
{
?>
<meta property="og:type" content="website" />
<meta property="og:title" content="<?= get_bloginfo('name') ?>"/>
<meta property="og:url" content="<?= get_site_url() ?>"/>
<meta property="og:image" content="<?= get_site_url() . '/wp-content/uploads/myhome.jpg' ?>"/> <!-- replace it with your static image-->
<?php
}
//for singles post page
else if (is_single() && !is_product())
{
if (has_post_thumbnail($post->ID))
{
$img_src = wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'medium');
}
//if featured image not present
else
{
$img_src = get_site_url() . '/wp-content/uploads/post.jpg'; //replace it with your static image
}
?>
<meta property="og:type" content="article" />
<meta property="og:title" content="<?= get_the_title($post->ID); ?>"/>
<meta property="og:url" content="<?= get_the_permalink($post->ID); ?>"/>
<meta property="og:image" content="<?= $img_src; ?>"/>
<?php
}
//for singles product page only
elseif (is_product())
{
$img_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'woocommerce_single_image_width'); //replace it with your desired size
?>
<meta property="og:type" content="product" />
<meta property="og:title" content="<?= get_the_title($post->ID); ?> by Pixel Komando"/>
<meta property="og:url" content="<?= get_the_permalink($post->ID); ?>" />
<meta property="og:image" content="<?= $img_url[0]; ?>"/>
<?php
}
//for product cat page
else if (is_product_category())
{
$term = get_queried_object();
$img_src = wp_get_attachment_url(get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true));
if (empty($img_src))
{
$img_src = get_site_url() . '/wp-content/uploads/myproductcat.jpg'; //replace it with your static image
}
?>
<meta property="og:type" content="object" />
<meta property="og:title" content="<?= $term->name; ?>" />
<meta property="og:url" content="<?= get_term_link($term->term_id, 'product_cat'); ?>" />
<meta property="og:image" content="<?= $img_src; ?>" />
<?php
}
//for shop page
elseif (is_shop())
{
?>
<meta property="og:title" content="<?= $term->name; ?>" />
<meta property="og:url" content="<?= get_permalink(woocommerce_get_page_id('shop')); ?>" />
<meta property="og:image" content="<?= get_site_url(); ?>/wp-content/uploads/myshop.jpg" /> <!-- replace it with your static image-->
<?php
}
else
{
return;
}
}
add_action('wp_head', 'wh_fb_opengraph', 5);
函数wh\u doctype\u opengraph($output){
返回$output。”
xmlns:og=”http://opengraphprotocol.org/schema/"
xmlns:fb=”http://www.facebook.com/2008/fbml"';
}
添加过滤器(“语言属性”、“wh\u doctype\u opengraph”);
函数wh_fb_opengraph()
{
全球$员额;
if(is_home()&&is_front_page())
{
?>
谢谢,这是我的第一个想法。