2017-08-17 46 views
0

試圖理解WP循環,我有點失落。邏輯上,每當有一個循環時就有一個集合。但是,這裏WP表示它在循環中顯示單個帖子。據我所知,當我想要使用WP循環時,例如,按照日期排序,在頭版中顯示所有帖子,如here。但是,當我只想要一個帖子時,WP循環如何工作?如何在循環中顯示單個帖子?

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

<h1><?php the_title(); ?></h1> 
    <?php the_content(); ?> 
<?php endwhile; else: ?> 
    <?php _e('Sorry, no pages matched your criteria.', 'textdomain'); ?> 
<?php endif; ?> 

該代碼如何輸出只有一個職位?

參考: https://developer.wordpress.org/themes/basics/the-loop/#individual-post

回答

0

認爲它像從數據庫表取出一個單個結果與1一個LIMIT子句。 您仍然使用while()循環,但只返回一個結果。

Wordpress然後使用the_title(),the_content()other tags獲得帖子的標題,內容等。

而這些標籤只是從post_title,post_contentwp_posts表中的其他列獲取值的函數。

例如,對於其他列:IDpost_authorpost_datepost_contentpost_titlepost_excerpt

+0

好,但與顯示多個職位沒有什麼區別....? https://developer.wordpress.org/themes/basics/the-loop/#blog-archive – Adelin

+0

不,沒有什麼區別。雖然循環保持不變,但您可以在循環內使用_get_template_part()_函數來創建自定義模板以顯示帖子。 – Aj334

+0

這就是讓我困惑的地方......循環是一樣的。什麼事告訴WP只在頁面上顯示一篇文章或顯示文章列表? – Adelin

相關問題