我绝对是WordPress/PHP开发新手,所以请原谅我的无知:-)
Q: How do I format a date/time returned from a custom query against $wpdb?
下面是一个场景:我正在尝试编写一个页面,其中显示有关我博客的一些简单统计信息。我展示的统计数据之一是最受欢迎的帖子,这是由评论数决定的。我基本上已经完成了查询,只是我一辈子都无法确定如何按我想要的方式设置发布日期/时间的格式!这里是代码(为长时间的窃听道歉):
<?php
$queryStr = "
SELECT
posts.ID,
posts.post_title,
posts.post_date,
posts.guid,
author.user_nicename as author,
count(comments.comment_ID) as comment_count
FROM
$wpdb->posts posts
INNER JOIN
$wpdb->comments comments
ON posts.ID = comments.comment_post_ID
INNER JOIN
$wpdb->users author
ON posts.post_author = author.ID
WHERE
posts.post_status = \'publish\'
AND posts.post_type = \'post\'
AND comments.comment_approved = 1
AND comments.comment_type = \'\'
GROUP BY
posts.ID
ORDER BY
count(comments.comment_ID) DESC,
posts.ID DESC
LIMIT
30
";
$popularPosts = $wpdb->get_results($queryStr);
if ($popularPosts) {
?>
<table class="stats_table">
<thead>
<tr>
<th>Post</th>
<th>Author</th>
<th>Posted At</th>
<th># comments</th>
</tr>
</thead>
<tbody>
<?php
foreach ($popularPosts as $popPost) {
?>
<tr>
<td><a href="<?= $popPost->guid ?>"><?= $popPost->post_title ?></a></td>
<td><?= $popPost->author ?></td>
<td><?= $popPost->post_date ?></td>
<td><?= $popPost->comment_count ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} else {
echo("<strong>No stories!</strong>\\n");
}
?>
很明显,这就是问题所在:
<td><?= $popPost->post_date ?></td>
我尝试了许多不同的方法,但没有一种能让我达到目的。这会得到我想要的格式,但使用了错误的日期(显然$wpdb在查询中没有返回时间戳):
<td><?= date(\'j M Y h:i A\', $popPost->post_date) ?></td>
我还尝试了以下解决方案:
<?php
global $popPost;
foreach ($popularPosts as $popPost) {
setup_postdata($popPost);
?>
<tr>
<td><a href="<?= $popPost->guid ?>"><?= $popPost->post_title ?></a></td>
<td><?= $popPost->author ?></td>
<td><?= the_time(\'j M Y h:i A\') ?></td>
<td><?= $popPost->comment_count ?></td>
</tr>
<?php
}
?>
但这只是重复
the page 而不是每篇文章的时间戳。
我做错了什么?