如果Wordpress今天有评论,请获取(第一个)评论链接



我有这个函数和一个到帖子的链接:

<?php
foreach ($results as $id) {
  $post = &get_post( $id->ID );
  setup_postdata($post);
  <li><a <?php href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
</li>
  <?php
} ?>

我想做的是:如果今天发布了一条评论,请向我显示已发布的第一条评论的链接。例如:如果今天有4条评论,我想要第一条评论的链接,而不是像现在这样的永久链接。

我尝试使用此:
<a <?php href="<?php the_permalink(get_comments->$post_id) ?>">postname</a>和类似comment_post_ID的变体,但我无法使其工作。我做错了什么?我该怎么办?

我认为您可能对对象和函数感到困惑(要么是错误的复制/粘贴)。当您尝试执行get_comments->$post_id时,这不会起作用,因为get_comment不是一个对象,get_comments是一个接受参数的函数。看看我在下面做了什么,因为它可能会对你有所帮助:

<?php
foreach ($results as $id) {
  $post = &get_post( $id->ID );
  setup_postdata($post);
  $args = array('post_id' => $id->ID, 'number' => 1);
  $lastComment = get_comments($args);
  if (!empty($lastComment[0]) and $lastComment[0]->comment_date > date('Y-m-d 00:00:00')){
    echo '<li><a href="'.get_comment_link($lastComment[0]).'">'.the_title().'</a></li>';
  }
?>

get_comments()将接受一堆争论,在上面的例子中,我给它传递了postID和计数1,所以它只提取最后一个注释。不过,它仍然会删除今天没有发表的评论,所以我添加的if条件应该只有在今天午夜后发布评论时才会回应。

最新更新