首页 > 解决方案 > 在 FB 和 FB OG 上共享时发送信息,通过使用 $_GET 的 URL 不起作用

问题描述

我创建了一个从 API 获取信息并动态显示的代码。

   foreach($filtered_listings as $listing){
     get_template_part( 'template-parts/listings/img/image','cover', $listing); 
   }

在该模板部分中,我以这种方式创建链接

<?php //Get all variables from arrays present in passed args ?> 
<?php extract($args); ?>
<a class="img-frame listings-img-frame" href="'.$home_url.'/annonce?annonceId='.$info['id'].'">

因此,当我在页面中时,我通过 $_GET 获取特定页面的 ID,并显示正确的信息。

  // filter_by_id is Declared in Functions.php
  $listing = array_filter($listings, "filter_by_id");
  $listing_key= key($listing);
  $this_listing= $listing[key($listing)];

所以所有这些都工作正常,在我的变量 $this_listing 中我有正确的信息并且可以以正确的方式显示它。

现在!我正在尝试在我的 function.php 文件中实现一些 FB 爬虫

function insert_fb_in_head() {
  global $post;
  // Import api data from correspondent file and set variables that will be used and retreived
  include_once "api/api-setup.php";
  global $listings;
  global $token; 

  function filter_by_id($listing) { 
    global $annonceId;
    $annonceId = $_GET['annonceId'];
    return $listing['info']['id'] == $annonceId;
  }
  $listing = array_filter($listings, "filter_by_id");
  $listing_key= key($listing);
  $this_listing= $listing[key($listing)];

  extract($this_listing);
  
  if ( !is_singular()) //if it is not a post or a page
    return;
    echo '<meta property="fb:app_id" content="Your Facebook App ID" />';
    echo '<meta property="og:title" content="'.$info['titre'].'"/>';
    echo '<meta property="og:type" content="article"/>';
    echo '<meta property="og:url" content="' . get_permalink() . '"/>';
    echo '<meta property="og:description" content="blablabla"/>';
    echo '<meta property="og:site_name" content="site Name"/>';
  if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
    $default_image="http://example.com/image.jpg"; //replace this with a default image on your server or an image in your media library
    echo '<meta property="og:image" content="' . $default_image . '"/>';
  }
  else{
    $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
    echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
  }
  echo "
  ";
  }
  add_action( 'wp_head', 'insert_fb_in_head', 5 );

这在我的网站上正常工作。如果我 var_dump($this_listing) 或我现在使用的唯一提取变量,例如 $info['titre'],我就会得到所需的信息。

但!当我以这种获取正确列表 ID 的方式共享 url 时:

    $annonceId;
    $annonceId = $_GET['annonceId'];

返回未定义变量的错误。

注意:未定义索引:第 64 行 /var/www/vhosts/wapinails.be/httpdocs/wp-content/themes/divi-child/functions.php 中的 annonceId 注意:未定义索引:/var/www/vhosts/ 中的 annonceId wapinails.be/httpdocs/wp-content/themes/divi-child/functions.php 第 64 行 注意:未定义索引:/v 中的 annonceId ...

所以我知道 $_GET 在这种情况下不是正确的方法。关于如何解决这个问题的任何建议?

标签: phpfacebookfacebook-opengraph

解决方案


推荐阅读