Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling function from within functions.php returns unwanted value
    primarykey
    data
    text
    <p>When I call the comment count function below from within my functions.php file, I get "0" even though the post has comments. When I call the function from another page template (<code>&lt;?php echo fb_comment_count(); ?&gt;</code>), the correct comment number shows.</p> <p>I have tried debugging to check the value of $post inside the fb_comment_count function and it generates the following:</p> <p><code>id = 818 id = 818 id = 818 id = 818 id = 818 id = 818 id = 818 id = 818</code></p> <p>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.</p> <p>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.</p> <p><img src="https://i.stack.imgur.com/pZZ1z.jpg" alt="gallery"></p> <p>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.</p> <p>Here is the fb_comment_count function.</p> <pre><code>// FB Comment Count (by Zack Austin) function fb_comment_count($link = 'link') { global $post; $url = 'https://graph.facebook.com/'; $posturl = get_permalink($post-&gt;ID); $url .= $posturl; $filecontent = wp_remote_retrieve_body(wp_remote_get($url, array('sslverify'=&gt;false))); $json = json_decode($filecontent); $count = $json-&gt;comments; if ($count == 0 || !isset($count)) { $count = 0; } $comments = $count; if ($count == 1) { $comments .= ''; } elseif ($count == 0) { $comments = '0'; } elseif ($count &gt; 1) { $comments .= ''; } if ($link == 'nolink') { return $comments; } else { return '&lt;a href="'.$posturl.'#comments" title="Comments for '.$post-&gt;post_title.'"&gt;'.$comments.'&lt;/a&gt;'; } } </code></pre> <p>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.</p> <pre><code>// 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' =&gt; 'ASC', 'orderby' =&gt; 'menu_order ID', 'id' =&gt; $post-&gt;ID, 'itemtag' =&gt; 'dl', 'icontag' =&gt; 'dt', 'captiontag' =&gt; 'dd', 'columns' =&gt; 3, 'size' =&gt; 'thumbnail', 'include' =&gt; '', 'exclude' =&gt; '' ), $attr)); $id = intval($id); if ( 'RAND' == $order ) $orderby = 'none'; if ( !empty($include) ) { $include = preg_replace( '/[^0-9,]+/', '', $include ); $_attachments = get_posts( array('include' =&gt; $include, 'post_status' =&gt; 'inherit', 'post_type' =&gt; 'attachment', 'post_mime_type' =&gt; 'image', 'order' =&gt; $order, 'orderby' =&gt; $orderby) ); $attachments = array(); foreach ( $_attachments as $key =&gt; $val ) { $attachments[$val-&gt;ID] = $_attachments[$key]; } } elseif ( !empty($exclude) ) { $exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); $attachments = get_children( array('post_parent' =&gt; $id, 'exclude' =&gt; $exclude, 'post_status' =&gt; 'inherit', 'post_type' =&gt; 'attachment', 'post_mime_type' =&gt; 'image', 'order' =&gt; $order, 'orderby' =&gt; $orderby) ); } else { $attachments = get_children( array('post_parent' =&gt; $id, 'post_status' =&gt; 'inherit', 'post_type' =&gt; 'attachment', 'post_mime_type' =&gt; 'image', 'order' =&gt; $order, 'orderby' =&gt; $orderby) ); } if ( empty($attachments) ) return ''; if ( is_feed() ) { $output = "\n"; foreach ( $attachments as $att_id =&gt; $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 &gt; 0 ? floor(100/$columns) : 100; $float = is_rtl() ? 'right' : 'left'; $selector = "gallery-{$instance}"; $output = apply_filters('gallery_style', " &lt;style type='text/css'&gt; #{$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; } &lt;/style&gt; &lt;!-- see gallery_shortcode() in wp-includes/media.php --&gt; &lt;div id='$selector' class='gallery galleryid-{$id}'&gt;"); $i = 0; foreach ( $attachments as $id =&gt; $attachment ) { $link = isset($attr['link']) &amp;&amp; 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false); $output .= "&lt;{$itemtag} class='gallery-item'&gt;"; $output .= "&lt;span class='photos-comment-number-wrap'&gt;&lt;span class='comment-number'&gt;". fb_comment_count() ."&lt;/span&gt;&lt;/span&gt;"; $output .= " &lt;{$icontag} class='gallery-icon'&gt; $link &lt;/{$icontag}&gt;"; if ( $captiontag &amp;&amp; trim($attachment-&gt;post_excerpt) ) { $output .= " &lt;{$captiontag} class='gallery-caption'&gt; " . wptexturize($attachment-&gt;post_excerpt) . " &lt;/{$captiontag}&gt;"; } $output .= "&lt;/{$itemtag}&gt;"; if ( $columns &gt; 0 &amp;&amp; ++$i % $columns == 0 ) $output .= '&lt;br style="clear: both" /&gt;'; } $output .= " &lt;br style='clear: both;' /&gt; &lt;/div&gt;\n"; return $output; } </code></pre> <p>Update:</p> <pre><code>// FB Comment Count (by Zack Austin) function fb_comment_count($link = 'link', $post) { $url = 'https://graph.facebook.com/'; $posturl = get_permalink($post-&gt;ID); $url .= $posturl; $filecontent = wp_remote_retrieve_body(wp_remote_get($url, array('sslverify'=&gt;false))); $json = json_decode($filecontent); $count = $json-&gt;comments; if ($count == 0 || !isset($count)) { $count = 0; } $comments = $count; if ($count == 1) { $comments .= ''; } elseif ($count == 0) { $comments = '0'; } elseif ($count &gt; 1) { $comments .= ''; } if ($link == 'nolink') { return $comments; } else { return '&lt;a href="'.$posturl.'#comments" title="Comments for '.$post-&gt;post_title.'"&gt;'.$comments.'&lt;/a&gt;'; } } // 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' =&gt; 'ASC', 'orderby' =&gt; 'menu_order ID', 'id' =&gt; $post-&gt;ID, 'itemtag' =&gt; 'dl', 'icontag' =&gt; 'dt', 'captiontag' =&gt; 'dd', 'columns' =&gt; 3, 'size' =&gt; 'thumbnail', 'include' =&gt; '', 'exclude' =&gt; '' ), $attr)); $id = intval($id); if ( 'RAND' == $order ) $orderby = 'none'; if ( !empty($include) ) { $include = preg_replace( '/[^0-9,]+/', '', $include ); $_attachments = get_posts( array('include' =&gt; $include, 'post_status' =&gt; 'inherit', 'post_type' =&gt; 'attachment', 'post_mime_type' =&gt; 'image', 'order' =&gt; $order, 'orderby' =&gt; $orderby) ); $attachments = array(); foreach ( $_attachments as $key =&gt; $val ) { $attachments[$val-&gt;ID] = $_attachments[$key]; } } elseif ( !empty($exclude) ) { $exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); $attachments = get_children( array('post_parent' =&gt; $id, 'exclude' =&gt; $exclude, 'post_status' =&gt; 'inherit', 'post_type' =&gt; 'attachment', 'post_mime_type' =&gt; 'image', 'order' =&gt; $order, 'orderby' =&gt; $orderby) ); } else { $attachments = get_children( array('post_parent' =&gt; $id, 'post_status' =&gt; 'inherit', 'post_type' =&gt; 'attachment', 'post_mime_type' =&gt; 'image', 'order' =&gt; $order, 'orderby' =&gt; $orderby) ); } if ( empty($attachments) ) return ''; if ( is_feed() ) { $output = "\n"; foreach ( $attachments as $att_id =&gt; $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 &gt; 0 ? floor(100/$columns) : 100; $float = is_rtl() ? 'right' : 'left'; $selector = "gallery-{$instance}"; $output = apply_filters('gallery_style', " &lt;style type='text/css'&gt; #{$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; } &lt;/style&gt; &lt;!-- see gallery_shortcode() in wp-includes/media.php --&gt; &lt;div id='$selector' class='gallery galleryid-{$id}'&gt;"); $i = 0; foreach ( $attachments as $att_id =&gt; $attachment ) { $link = isset($attr['link']) &amp;&amp; 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false); $output .= "&lt;{$itemtag} class='gallery-item'&gt;"; $output .= "&lt;span class='photos-comment-number-wrap'&gt;&lt;span class='comment-number'&gt;". fb_comment_count('link', $attachment) ."&lt;/span&gt;&lt;/span&gt;"; $output .= " &lt;{$icontag} class='gallery-icon'&gt; $link &lt;/{$icontag}&gt;"; $output .= "&lt;/{$itemtag}&gt;"; if ( $columns &gt; 0 &amp;&amp; ++$i % $columns == 0 ) $output .= '&lt;br style="clear: both" /&gt;'; } $output .= " &lt;br style='clear: both;' /&gt; &lt;/div&gt;\n"; return $output; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload