GET_COMMENTS与POST_ID不一致

时间:2014-06-10 作者:Totoro

我正在尝试从另一个帖子中获得评论。我正在获取post\\u id并检查它是否有评论

我每次都能得到当前帖子的评论,但对于另一篇帖子,我得到的结果参差不齐(我确实做到了,但无法重现结果)

这是我正在处理的代码,一些替代尝试被注释掉了:

if(get_comments_number($getLangsPostID) > 0){
    //echo "<pre>This post id is ".$thisPostID." The other language post id is (".$getLangsPostID."):";
    //var_dump($getLangsPostID);
    $altLangReq = array(
        \'post_id\' => $getLangsPostID // have tried with and without trailing comma
    );

    //$langComments = get_comments($altLangReq);
    $langComments = get_comments(\'post_id=\'.$getLangsPostID);

    echo "<p>Current post ($thisPostID), request for post id (".$getLangsPostID.") is:<pre>";
    var_dump($langComments);
    //$x = get_comments(\'post_id=31\'); // hard coded also fails
    $x = get_comments(array(\'post_id\'=>31));
    var_dump($x);
    echo "</pre></p>";
如果有关系的话,我正在使用polylang,并试图在每个语言帖子上显示所有语言备选方案的评论。我正在使用两个帖子(id 21和31),每个帖子都有3或4条评论(这些是通过get\\u comments\\u number找到的)。

我也在评论圈之外。略为定制的下划线主题中的php模板。

编辑:

一些澄清,我想描述大多数情况,如果有其他因素,但基本上如果我打电话:

var_dump(get_comments(\'post_id=21\')); // 21 is the current post
如果我打电话给:

var_dump(get_comments(\'post_id=31\')); // 31 is a different post
我得到一个空数组,如果我转到post 31,那么31工作,21返回一个空数组,但这似乎是调用post而不是当前post的方法(通过post\\u id)。See the first example in the WP codex

编辑2答案:(我现在添加这个,因为我在8小时的ATM机上无法回答我自己的问题)

原来是语言插件。。。当我调用注释时,它会添加一个语言过滤器:

\'lang\' => $currentLang
所以我必须明确地覆盖它:

get_comments(array(\'post_id\'=>31, \'lang\' => $targetLang));
我仍然不知道为什么我有时能在没有这个的情况下得到其他评论。

2 个回复
最合适的回答,由SO网友:Totoro 整理而成

原来是我使用的语言插件(polylang)。。。当我调用注释时,它会添加一个语言过滤器:

\'lang\' => $currentLang
所以我必须明确地覆盖它:

get_comments(array(\'post_id\'=>31, \'lang\' => $targetLang));
我仍然不知道为什么我有时能在没有这个的情况下得到其他评论。

SO网友:kdgilang

帖子必须有评论。

$comments = get_comments( array( \'post_id\' => $id ) );

if( empty( $comments ) ) {
    return;
}

foreach( $comments as $val ) { 
   echo $val->comment_content;
}

结束

相关推荐

COMMENTS_POPUP_LINK()-如何根据不同的评论条件加载不同的类/图片?

作为Codex 表示语法:<?php comments_popup_link( $zero, $one, $more, $css_class, $none ); ?> 很明显,我们可以根据不同的注释条件加载不同的文本,如“注释打开”、“1注释”、“2+注释”和“注释关闭”,此外,我们还可以将CSS类加载到该部分。我想从具有不同注释条件的图像精灵加载不同的图像:评论打开但未发布时-发布评论时加载一条-关闭评论时加载一条-加载一条What I thought。。。就是加载不同的类,这样我