Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to customize the output of the [gallery] shortcode to include comment count by this plugin: Facebook Comments for WordPress plugin

I found a function by Zack Austin (shown below) that retrieves the number of comments from the Facebook Graph. Furthermore, I found a piece of code by t31os (also shown below) that allows me to customize the output of the [gallery] shortcode. So therefore, I am trying to incorporate the Facebook comment count function into the customizable gallery code.

Now, when I insert both pieces of code into my functions.php and call the function from one of my other page templates (<?php echo fb_comment_count(); ?>), it works fine.

However, when I try calling the function from within functions.php, I get a "0" no matter how many comments there are.

I have tried debugging to check the value of $post inside the fb_comment_count function and it generates the following:

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

This is wrong since each of those ids should be different for each of the attachment pages the image is on. 818 is the id of the entire gallery page itself. I suppose this is why I keep getting "0" for the comment count since the gallery page (id 818) itself has no comments.

To give a better idea of what I'm trying to do, here is a template for the gallery page. Each of those images are linked to their respective attachment pages. The white box with the number inside is the comment count.

gallery

If anybody can help me figure out how to retrieve the correct comment count for each of the images in the gallery, I'd be very grateful. Thank you.

Here is the fb_comment_count function by Zack Austin.

// 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>';
  }
}

And here is the custom gallery code where I'm calling the fb_comment_count function above (about 22 lines from the bottom). Both the code below and the code above are in my functions.php file.

// 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;
}
share|improve this question
    
try passing the post->ID as a parameter to the fb_comment_count function... I am guessing, it will solve the problem... –  booota Feb 2 '12 at 6:28
add comment

1 Answer

Inside this loop:

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" />';
}

You can get at the comment count directly from the object:

$attachment->comment_count

Refer to the wp_posts table in the Database Description Codex entry for more information.

share|improve this answer
    
I tried changing the gallery-caption output to this: $output .= "<{$captiontag} class='attachment-comment-count'> " . wptexturize($attachment->comment_count) . " </{$captiontag}>"; but it still shows the wrong comment count. It just shows zero. –  jwp Jul 19 '11 at 18:53
    
I have rearranged my question to be more specific to what I cannot get accomplished. If you can take another look, I'd really appreciate it. –  jwp Jul 20 '11 at 18:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.