2015-06-16 57 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

那麼,在你的答案解釋,然後。 –

相關問題