【问题标题】:Modifying code to get recent commented posts above older post修改代码以获取旧帖子上方的最新评论帖子
【发布时间】:2019-09-26 12:04:06
【问题描述】:

我正在努力完成这项工作,但它并不完全有效。这是我目前查询帖子的方式:

<?php 
    // the query
    $the_query = new WP_Query(  array( 'posts_per_page' => -1 ) ); 
    if ( $the_query->have_posts() ) : 
    ?>
        <!-- pagination here -->
        <!-- the loop -->
        <?php 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
        ?>                          
            <li data-href="<?php $zlink = get_the_permalink(); echo preg_replace("#/$#im", '', $zlink);?>">
                <div>
                    <a class="button" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </div>

我想要达到的目标如下:

  1. 在发布日期较新的帖子之上显示最近评论的帖子...
  2. 如果博客文章有评论,无论该博客文章有多旧,我都希望该博客文章位于较新的博客文章之上,如果较新的博客文章没有更新(意思是:没有最近的 cmets)。

我首先在顶部显示最近评论的帖子(见下文),但这完全忽略了一个事实,即有些帖子没有 cmets,我找不到将两者结合并显示在一个列表中的方法。

<?php
    $args = array(
    'status' => 'approve',
    'number' => 6,
    'order' => 'DESC'
);
$comments = get_comments($args);

foreach($comments as $comment) : $count++;

        $post_args = array(
            'post_type' => 'post',
            'p' => $comment->comment_post_ID,
            'posts_per_page' => 1
            );

        $posts = get_posts($post_args);

        foreach($posts as $post) : setup_postdata($post);

            the_title();
        endforeach;

endforeach;
?>

有人可以帮忙吗?

【问题讨论】:

  • 您使用的查询显示最新的 cmets?你到底有什么问题请解释一下?
  • 你能明确解释一下你的排序标准吗?示例:如果一篇博文有评论,无论该博文有多旧,我都希望该博文位于较新的博文之上,前提是该博文没有 cmets。
  • 没错,@pendo。这是我唯一想要的。所以你可以有 1. old-post-while-has-most-recent-comment-among all-posts 2. latest post 3. old-post-and-not-recently-commented (not before latest post date) 等等在...
  • 您能检查一下我发布的解决方案,看看是否满足您的需求?
  • 我检查过了,@pendo。请在您的答案下方查看我的反应...

标签: php mysql wordpress post while-loop


【解决方案1】:

下面创建一个空数组,将所有带有 cmets 的帖子添加到该数组中。然后它添加所有没有 cmets 的帖子。最后,它根据评论日期或发布日期对它们进行排序,具体取决于该帖子是否有任何 cmets

//create an array to stuff all your posts in
$arrAllPosts = array();

$args = array(
    'post_type'         => 'post',  //show only posts (not pages, etc)
    'comment_count'     => array(   //pass it an array
        'value'     => 1,           //value is 1, with compare means greater than or equal to 1
        'compare'   => '>='
    ),
    'posts_per_page'    => -1       //gimme all of them
);
$postsWithComments = new WP_Query($args);


while($postsWithComments->have_posts()) {$postsWithComments->the_post();
    //get the comments for this post
    $comments = get_comments(array(
        'post_id'   => get_the_ID(),    //pass the post ID
        'orderby'   => 'comment_date',  //tell it to sort by the comment date, so you only get the latest
        'number'    => 1                //just get the latest
    ));
    foreach($comments as $comment) { //we're only looping this once, since there is only one
        $arrAllPosts[] = array(
            'dateToSortBy'  => $comment->comment_date,  //we'll use comment date to sort by later, instead of the post date
            'the_post_obj'  => $post                    //add the global post object, which is currently set to the current post
        );
    }
}
//now we get the posts with no comments
$args = array(
    'post_type'         => 'post',  //Only posts (not pages, etc)
    'comment_count'     => 0,       //Posts with no comments only
    'posts_per_page'    => -1       //gimme all of them
);

$postsNoComments = new WP_Query($args); //run it

while($postsNoComments->have_posts()) {$postsNoComments->the_post();    //loop it
    $arrAllPosts[] = array(
        'dateToSortBy'  => $post->post_date,  //we'll use the post date to sort by
        'the_post_obj'  => $post            //add the global post object, which is currently set to the current post
    );

}

function date_compare($a, $b) { //create a custom function to sort the array by the date of the post or the date of the comment
    $tmA = strtotime($a['dateToSortBy']);   //convert to time
    $tmB = strtotime($b['dateToSortBy']);   //convert to time
    return ($tmA < $tmB)?true:false;
}
usort($arrAllPosts, 'date_compare');

//Display the title for each post, from the sorted list
foreach($arrAllPosts as $curPost) { 
    $post = $curPost['the_post_obj'];   //make this the global post object
    setup_postdata($post);              //setup the data
    echo "<a href='" . get_the_permalink() . "'>" . get_the_title() . "</a>";
} 

【讨论】:

  • 潘多,哇!好的,一个一个:所以这替换了哪个代码?如何准确地循环使用我的列表项?
  • 我添加了一个 foreach 循环,它遍历您的帖子并输出标题,就像您的示例代码一样。这有助于说清楚吗?
  • 哇!快到了,pendo... 问题是没有链接。这些帖子只是纯文本(标题),但没有链接到它们的 URL 或超链接。你也可以检查一下吗?
  • 原来的代码就是这样,所以我重新创建了它。您在寻找 the_permalink(); ? codex.wordpress.org/Function_Reference/the_permalink
  • 嗯。然而,我确实吐出了永久链接/网址(另请参阅代码,它就在那里)。我到底需要改变什么才能让链接现在工作?在哪里添加永久链接?
猜你喜欢
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多