2015-06-16 56 views
-1

我正在尝试在我的博客上获取最新帖子。这些显示在我的网站首页上的一个小部件中。虽然我有点麻烦。帖子越来越多。 http://goo.gl/Q2STSC,这是我的SQL查询。在我的网页上从我的wordpress获取最新帖子

$posts = DB::connection('blog')->select("  
SELECT a.post_title AS title, a.post_name AS slug, meta_value AS thumbnail,a.post_content AS contenido, a.post_date AS fecha 
FROM wp_postmeta, wp_posts AS a 
JOIN wp_posts AS b ON a.ID = b.post_parent 
WHERE b.ID = wp_postmeta.post_id 
    AND meta_key = '_wp_attached_file' AND a.post_status = 'publish' 
ORDER BY fecha 
LIMIT 0 , 6"); 
+1

请解释为什么你正在使用一个自定义的sql查询。在wordpress中有正确的方式来做到这一点 –

回答

-2

试试这个

<?php $args = array(
'posts_per_page' => 6, 
'offset'   => 0, 
'orderby'   => 'date', 
'order'   => 'DESC', 
'post_type'  => 'post', 
'post_status'  => 'publish' 
); 
$posts_array = get_posts($args); 
foreach ($posts_arrayas $post) : setup_postdata($post); ?> 
    <li> 
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
    </li> 
<?php endforeach;?> 

请使用上面的代码从你blog.In取上面的代码中最新的帖子,你可以看到不同的参数

posts_per_page:职位数来获得
偏移量:起始邮政编号likeyou在自定义查询限制0 [偏移量],6 [posts_per_page]
顺序:排序[DESC/ASC]
orderby:date [订单子句必须执行的列]
post_type:post(帖子的类型)(如果您想获取新闻等自定义帖子的帖子,则可以将新闻传递为post_type
post_status:publish后的状态)中的foreach

您也可以永久链接[帖子的链接]和the_title的帖子(标题)

欲了解更多详情,您可以点击这里https://codex.wordpress.org/Function_Reference/get_posts

+3

请编辑你的答案,告诉我们他为什么要这样做。他应该如何运用你的建议?它会改变什么? –

+0

按照他的文章,他试图获得最新的帖子,因为他正在尝试自定义查询..所以我已经建议WP默认功能,这将给出相同的结果,因为他期待..他只需要复制此代码,并需要以循环产生的数组 – Nitin

+1

那么,在你的答案解释,然后。 –

相关问题