如何设置全局$ post变量?

时间:2011-07-20 03:28:29

标签: php wordpress global-variables

此处未设置全局$ post变量。我该如何设置?

function fb_comment_count($link = 'link') {
  global $post;
  $url = 'https://graph.facebook.com/';
  $posturl = get_permalink($post->ID);
  $url .= $posturl;

  $filecontent = wp_remote_retrieve_body(wp_remote_get($url, array('sslverify'=>false)));
  $json = json_decode($filecontent);
  $count = $json->comments;
  if ($count == 0 || !isset($count)) {
    $count = 0;
  }

  $comments = $count;
  if ($count == 1) {
    $comments .= '';
  }
  elseif ($count == 0) {
    $comments = '0';
  }
  elseif ($count > 1) {
    $comments .= '';
  }
  if ($link == 'nolink') {
    return $comments;
  }
  else {
    return '<a href="'.$posturl.'#comments" title="Comments for '.$post->post_title.'">'.$comments.'</a>';
  }
}

1 个答案:

答案 0 :(得分:0)

你“设置”它的方式与设置常规(非全局)变量的方式相同,使用赋值运算符 - 等于字符(=)。

http://php.net/manual/en/language.operators.assignment.php

您还可以使用$ GLOBALS []数组。有关全局关键字实际执行的操作,请参阅文档:

http://php.net/manual/en/language.variables.scope.php

相关问题