从函数内调用函数。php返回不需要的值

时间:2011-07-19 作者:jwp

我正在尝试自定义[库]快捷码的输出,以包含此插件的评论计数:Facebook Comments for WordPress plugin

我找到了一个function by Zack Austin (如下所示)从Facebook图表中检索评论数。此外,我还发现了一块code by t31os (如下所示)允许我自定义[库]快捷码的输出。因此,我试图将Facebook评论计数功能合并到可定制的库代码中。

现在,当我将这两段代码插入到函数中时。并从我的另一个页面模板中调用该函数(<?php echo fb_comment_count(); ?>), 它工作得很好。

但是,当我尝试从函数内部调用函数时。php,无论有多少条评论,我都会得到一个“0”。

我尝试在fb\\u comment\\u count函数中调试$post的值,它生成以下内容:

id = 818 id = 818 id = 818 id = 818 id = 818 id = 818 id = 818 id = 818

这是错误的,因为图像所在的每个附件页的每个ID都应该不同。818是整个库页面本身的id。我想这就是为什么我的评论数一直为“0”,因为库页面(id 818)本身没有评论。

为了更好地了解我正在尝试做什么,这里有一个图库页面的模板。每个图像都链接到各自的附件页。带有数字的白色框是注释计数。

gallery

如果有人能帮我找出如何检索图库中每个图像的正确评论数,我将不胜感激。非常感谢。

下面是Zack Austin的fb\\u comment\\u count函数。

// FB Comment Count (by Zack Austin)
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>\';
  }
}
这里是自定义库代码,我在这里调用上面的fb\\u comment\\u count函数(从底部开始大约22行)。下面的代码和上面的代码都在我的函数中。php文件。

// Custom Gallery
add_filter( \'post_gallery\', \'my_post_gallery\', 10, 2 );
function my_post_gallery( $output, $attr) {
    global $post, $wp_locale;

    static $instance = 0;
    $instance++;

    // We\'re trusting author input, so let\'s at least make sure it looks like a valid orderby statement
    if ( isset( $attr[\'orderby\'] ) ) {
        $attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
        if ( !$attr[\'orderby\'] )
            unset( $attr[\'orderby\'] );
    }

    extract(shortcode_atts(array(
        \'order\'      => \'ASC\',
        \'orderby\'    => \'menu_order ID\',
        \'id\'         => $post->ID,
        \'itemtag\'    => \'dl\',
        \'icontag\'    => \'dt\',
        \'captiontag\' => \'dd\',
        \'columns\'    => 3,
        \'size\'       => \'thumbnail\',
        \'include\'    => \'\',
        \'exclude\'    => \'\'
    ), $attr));

    $id = intval($id);
    if ( \'RAND\' == $order )
        $orderby = \'none\';

    if ( !empty($include) ) {
        $include = preg_replace( \'/[^0-9,]+/\', \'\', $include );
        $_attachments = get_posts( array(\'include\' => $include, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );

        $attachments = array();
        foreach ( $_attachments as $key => $val ) {
            $attachments[$val->ID] = $_attachments[$key];
        }
    } elseif ( !empty($exclude) ) {
        $exclude = preg_replace( \'/[^0-9,]+/\', \'\', $exclude );
        $attachments = get_children( array(\'post_parent\' => $id, \'exclude\' => $exclude, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
    } else {
        $attachments = get_children( array(\'post_parent\' => $id, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
    }

    if ( empty($attachments) )
        return \'\';

    if ( is_feed() ) {
        $output = "\\n";
        foreach ( $attachments as $att_id => $attachment )
            $output .= wp_get_attachment_link($att_id, $size, true) . "\\n";
        return $output;
    }

    $itemtag = tag_escape($itemtag);
    $captiontag = tag_escape($captiontag);
    $columns = intval($columns);
    $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
    $float = is_rtl() ? \'right\' : \'left\';

    $selector = "gallery-{$instance}";

    $output = apply_filters(\'gallery_style\', "
        <style type=\'text/css\'>
            #{$selector} {
                margin: auto;
            }
            #{$selector} .gallery-item {
                float: {$float};
                margin-top: 10px;
                text-align: center;
                width: {$itemwidth}%;           }
            #{$selector} img {
                border: 2px solid #cfcfcf;
            }
            #{$selector} .gallery-caption {
                margin-left: 0;
            }
        </style>
        <!-- see gallery_shortcode() in wp-includes/media.php -->
        <div id=\'$selector\' class=\'gallery galleryid-{$id}\'>");

    $i = 0;
    foreach ( $attachments as $id => $attachment ) {
        $link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

        $output .= "<{$itemtag} class=\'gallery-item\'>";
        $output .= "<span class=\'photos-comment-number-wrap\'><span class=\'comment-number\'>". fb_comment_count() ."</span></span>";
        $output .= "
            <{$icontag} class=\'gallery-icon\'>
                $link
            </{$icontag}>";
        if ( $captiontag && trim($attachment->post_excerpt) ) {
            $output .= "
                <{$captiontag} class=\'gallery-caption\'>
                " . wptexturize($attachment->post_excerpt) . "
                </{$captiontag}>";
        }
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= \'<br style="clear: both" />\';
    }

    $output .= "
            <br style=\'clear: both;\' />
        </div>\\n";

    return $output;
}

1 个回复
SO网友:Chip Bennett

在此循环内:

foreach ( $attachments as $id => $attachment ) {
        $link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

        $output .= "<{$itemtag} class=\'gallery-item\'>";
        $output .= "<span class=\'photos-comment-number-wrap\'><span class=\'comment-number\'>". comments_number() ."</span></span>";
        $output .= "
            <{$icontag} class=\'gallery-icon\'>
                $link
            </{$icontag}>";
        if ( $captiontag && trim($attachment->post_excerpt) ) {
            $output .= "
                <{$captiontag} class=\'gallery-caption\'>
                " . wptexturize($attachment->post_excerpt) . "
                </{$captiontag}>";
        }
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= \'<br style="clear: both" />\';
}
您可以直接从对象获取注释计数:

$attachment->comment_count
参见the wp_posts table in the Database Description Codex entry 了解更多信息。

结束