我创建了一个文件,该文件应该从我们的wordpress博客中获取最新的3个博客,并以DESC顺序将它们发布到我们网站的主页上。然而,正在发生的事情是三个中最古老的博客首先显示的是最新的,然后是第二个最新的博客。最旧的应该是最后一个显示。
$sql="SELECT title, post_name, date, content, CONCAT(LEFT(image, LENGTH(image) - LOCATE('.', REVERSE(image))),'-500x375.',SUBSTRING_INDEX(image, '.', -1)) AS image
FROM (
SELECT
p.post_title AS title,
p.post_status AS 'status',
p.post_date_gmt AS date,
p.post_content AS content,
p.post_name AS post_name,
(SELECT `guid` FROM w_posts WHERE id = m.meta_value) AS image
FROM w_posts p, w_postmeta m
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND p.id = m.post_id
AND m.meta_key = '_thumbnail_id'
ORDER BY date DESC
LIMIT 3
) TT";?>
<div class="col-xs-12"><hr><h2 class="title-most-off"><a href="https://www.example.com/blogs/">Latest Blog Posts</a></h2> </div>
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$title=$row['title'];
$shortTitle = substr($title, 0, 100);
$content=$row['content'];
$preview = substr($content, 0, 175);
$date=$row['date'];
$name=$row['post_name'];
$image=$row['image'];?>
<div class="col-xs-12 col-md-4 post">
<a href="https://www.example.com/blogs/<?php echo $name; ?>" target="_blank">
<img class="img-responsive fw" src="<?php echo $image; ?>" />
<div class="caption">
<h4 class="blog-post-title"><?php echo $shortTitle; ?></h4>
</div>
</a>
</div>
<?php }
}?>
不确定为什么订单不正确,当我在phpmyadmin中运行查询时,即使第一个显示结果的日期在第二个和第三个显示结果之前,顺序也是相同的。所以结果是第3-1st-2nd而不是1st-2nd-3rd
答案 0 :(得分:0)
事实证明代码一直都是正确的,导致问题的其中一个帖子上的日期存在问题。
感谢大家的帮助。