2017-08-14 3 views
1

でダイナミックFacebookのOGを作り、ここに私は私のWordpress/Woocommerceヘッダーに何をやったかです私woocommerce製品ページでは、私はショップページを共有したいとき、FBデバッガが私にこの例を示しますは右の画像と私はFacebook上で共有し、右リンク毎回を持つためにWoocommerce

URL:https://www.pixelkomando.com/shop

メタタグのOG:URL https://www.pixelkomando.com/shop/CATEGORY/PRODUCT/

0を

ショップページ自体を除いて、どこでも正しく動作するようです。ショップページのURLを取得する代わりに、ランダムな製品のURLを指定します。

本当に何が間違っているのかわかりません。

よろしく フェロ

答えて

2

それはあなたがget_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); 

コードがテストされ、動作します。

0

問題があなたのOGであるように思える:URLタグ。私がsharing debuggerで再掻き回すたびにそれは異なっています。私には、これはget_permalink()メソッドが一貫した結果を返さないことを示唆しています。

FYIのog:urlメタタグは必須ではないので、ここで簡単に修正することができます。同じリソースにアクセスする複数のURLがあり、FBのクローラがURLであることを標準的なものにしたい場合は、本当に必要です。

+0

ありがとう、それは私の最初のアイデアでした。 – Vfero

関連する問題