简体   繁体   English

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

[英]Calling function from within functions.php returns unwanted value

When I call the comment count function below from within my functions.php file, I get "0" even though the post has comments.当我从我的functions.php文件中调用下面的评论计数function时,即使帖子有评论,我也会得到“0”。 When I call the function from another page template ( <?php echo fb_comment_count(); ?> ), the correct comment number shows.当我从另一个页面模板( <?php echo fb_comment_count(); ?> )调用 function 时,会显示正确的评论编号。

I have tried debugging to check the value of $post inside the fb_comment_count function and it generates the following:我尝试调试检查 fb_comment_count function 中 $post 的值,它生成以下内容:

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.这是错误的,因为图像所在的每个附件页面的每个 id 都应该不同。 818 is the id of the entire gallery page itself. 818 是整个图库页面本身的 id。 I suppose this is why I keep getting "0" for the comment count since the gallery page (id 818) itself has no comments.我想这就是为什么我的评论计数一直为“0”,因为图库页面(id 818)本身没有评论。

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.里面有数字的白框是评论数。

画廊

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.这是 fb_comment_count function。

// 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).这是自定义库代码,我在上面调用 fb_comment_count function(距底部约 22 行)。 Both the code below and the code above are in my functions.php file.下面的代码和上面的代码都在我的functions.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;
}

Update:更新:

// FB Comment Count (by Zack Austin)
function fb_comment_count($link = 'link', $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>';
  }
}

// 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 $att_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('link', $attachment) ."</span></span>";
        $output .= "
            <{$icontag} class='gallery-icon'>
                $link
            </{$icontag}>";
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= '<br style="clear: both" />';
    }

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

    return $output;
}

you should call function fb_comment_count($link = 'link') { and pass in the atachemnt post object so it would look like function fb_comment_count($link = 'link', $post) { , and remove the global $post just after. you should call function fb_comment_count($link = 'link') { and pass in the atachemnt post object so it would look like function fb_comment_count($link = 'link', $post) { , and remove the global $post just after.

this should be called from within the foreach loop that generates the gallery, just here:这应该从生成画廊的 foreach 循环中调用,就在这里:

 foreach ( $attachments as $att_id => $attachment )
       fb_comment_count('link', $attachment);

also, you should really post wordpress question over at wordpress.stackexchange.com另外,您真的应该在 wordpress.stackexchange.com 上发布 wordpress 问题

You can make the function fb_comment_count call a optional variable, here I will call it $id, which will contains the ID of the attachement (optional so it will not break the function in other areas of site).您可以将 function fb_comment_count 调用为可选变量,这里我将其称为 $id,其中包含附件的 ID(可选,因此不会破坏站点其他区域的 function)。 Then with that ID we can get the attachment page's URL with get_attachment_link, is in that page that we'll find the comments for attachment.然后使用该 ID,我们可以使用 get_attachment_link 获取附件页面的 URL,在该页面中,我们将找到附件的注释。 The code should be something like:代码应该是这样的:

<?php
// FB Comment Count (by Zack Austin)
// Changes by Bruno Cantuaria (bruno@cantuaria.net.br)
function fb_comment_count($link = 'link', $id = 0) {
    global $post;

    $url = 'https://graph.facebook.com/';
    if ($id > 0)
        $posturl = get_attachment_link($id);
    else
        $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>';
    }
}

// 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('link', $id) ."</span></span>";
        $output .= "
            <{$icontag} class='gallery-icon'>
                $link
            </{$icontag}>";
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= '<br style="clear: both" />';
    }

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

    return $output;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM