2

我有一个像下面这样的功能

if(!function_exists('generate_share_urls')) {
  function generate_share_urls() {
      $title = the_title();
      $content = the_content();
    $share_urls = array (
      'facebook'      => 'http://www.facebook.com/sharer/sharer.php?u=',
      'twitter'       => 'http://twitter.com/share?url=',
      'google_plus'   => 'https://plus.google.com/share?url=',
      'linkedin'      => 'http://www.linkedin.com/shareArticle?mini=true&url=',
      'pinterest'     => 'http://pinterest.com/pin/create/button/?url=',
      'email'         => 'mailto:?subject={'.$title.'}&body='.$content.',
      'permalink'     => ''
   );
   return $share_urls;
  }
}

现在,当运行该功能时,我找不到页面的 $title 和 $content。我的代码有什么问题吗?

4

1 回答 1

2

the_title()并将打印标题/内容,而不是返回它the_content()。您需要的是以下功能:get_

if(!function_exists('generate_share_urls')) {
  function generate_share_urls() {
      $title = get_the_title();
      $content = get_the_content();
    $share_urls = array (
      'facebook'      => 'http://www.facebook.com/sharer/sharer.php?u=',
      'twitter'       => 'http://twitter.com/share?url=',
      'google_plus'   => 'https://plus.google.com/share?url=',
      'linkedin'      => 'http://www.linkedin.com/shareArticle?mini=true&url=',
      'pinterest'     => 'http://pinterest.com/pin/create/button/?url=',
      'email'         => 'mailto:?subject={'.$title.'}&body='.$content.',
      'permalink'     => ''
   );
   return $share_urls;
  }
}
于 2013-01-14T08:00:17.407 回答