我正在尝试从另一个帖子中获得评论。我正在获取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));
我仍然不知道为什么我有时能在没有这个的情况下得到其他评论。