循环获取 Wordpress 页面的 "latest comment date"
Get "latest comment date" of a Wordpress page in a loop
有没有简单的方法获取wordpress页面的“最新评论日期”?
对于页面本身,有一个像这样的简单解决方案:
get_the_date('Y-m-d', $post->ID)
例如,这对我不起作用(特别是因为我也无法定义最后一条评论):
get_comment_date( 'Ymd', $post->ID);
而且我的阵列方式不起作用。 “comment_count
”没问题,但对于所有页面,“get_comment_date( 'd\/m\/Y' )
”始终是同一日期 - 为什么?
$args = array(
'post_id' => $post->ID,
'posts_per_page' => 10,
'post_type' => 'page',
'orderby' => 'date',
'category_name' => 'MyName');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<?php echo '<div class="comments ' . get_comment_date( 'd\/m\/Y' ) . '"><span>'. $comments_count .'<span></div>'; ?>
<?php endforeach; ?>
如果我没有理解错的话,您是在尝试获取特定页面的最新评论。所以,我会使用 get_comments
Docs 函数。
$comment_args = array(
'post_type' => 'page',
'post_id' => get_the_id(), // Or if you're in a page loop, you could use $post->ID instead of get_the_id()
'number' => 1,
);
$latest_comment = get_comments($comment_args);
// This would give you the latest comment
print_r($latest_comment[0]->comment_content);
// This would give you the comment date
print_r($latest_comment[0]->comment_date);
// This would give you the comment date in gmt
print_r($latest_comment[0]->comment_date_gmt);
请注意 'number' => 1,
将检索最新的评论。
因此,带有页面循环的整个代码将如下所示:
$args = array(
'posts_per_page' => 10,
'post_type' => 'page',
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
the_title();
$comment_args = array(
'post_id' => get_the_ID(),
'number' => 1,
);
$latest_comment = get_comments($comment_args);
?>
<div class="comments"><?php echo $latest_comment[0]->comment_date; ?></div>
<hr>
<?php
};
wp_reset_postdata();
结果如下:
如果这就是您要找的,请告诉我!
有没有简单的方法获取wordpress页面的“最新评论日期”?
对于页面本身,有一个像这样的简单解决方案:
get_the_date('Y-m-d', $post->ID)
例如,这对我不起作用(特别是因为我也无法定义最后一条评论):
get_comment_date( 'Ymd', $post->ID);
而且我的阵列方式不起作用。 “comment_count
”没问题,但对于所有页面,“get_comment_date( 'd\/m\/Y' )
”始终是同一日期 - 为什么?
$args = array(
'post_id' => $post->ID,
'posts_per_page' => 10,
'post_type' => 'page',
'orderby' => 'date',
'category_name' => 'MyName');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<?php echo '<div class="comments ' . get_comment_date( 'd\/m\/Y' ) . '"><span>'. $comments_count .'<span></div>'; ?>
<?php endforeach; ?>
如果我没有理解错的话,您是在尝试获取特定页面的最新评论。所以,我会使用 get_comments
Docs 函数。
$comment_args = array(
'post_type' => 'page',
'post_id' => get_the_id(), // Or if you're in a page loop, you could use $post->ID instead of get_the_id()
'number' => 1,
);
$latest_comment = get_comments($comment_args);
// This would give you the latest comment
print_r($latest_comment[0]->comment_content);
// This would give you the comment date
print_r($latest_comment[0]->comment_date);
// This would give you the comment date in gmt
print_r($latest_comment[0]->comment_date_gmt);
请注意 'number' => 1,
将检索最新的评论。
因此,带有页面循环的整个代码将如下所示:
$args = array(
'posts_per_page' => 10,
'post_type' => 'page',
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
the_title();
$comment_args = array(
'post_id' => get_the_ID(),
'number' => 1,
);
$latest_comment = get_comments($comment_args);
?>
<div class="comments"><?php echo $latest_comment[0]->comment_date; ?></div>
<hr>
<?php
};
wp_reset_postdata();
结果如下:
如果这就是您要找的,请告诉我!