關鍵函數:
have_posts():
have_posts() 默認是一個全局函數;
have_posts函數被調用時實際上是調用全局變量$wp_query->have_posts()成員函數,來簡單檢查一個全局數組(array)變量$posts的一個循環計數器,以確認是否還有post,如果有返回true(1),如果沒有返回false(0)。
the_post()
the_post()函數則調用$wp_query->the_post()成員函數前移循環計數器,並且創建一個全局變量$post(不是$posts),把當前的post的所有信息都填進這個$post變量中,以備接下來使用。比如:
the_ID()
post_class()
get_the_title()
the_content()
Template Tags(在循環內使用)
簡單的使用可以通過函數來直接執行,如the_content()直接顯式post的內容,the_title()顯式帖子的標題,the_time()顯示帖子的時間等WORDPRESS的Template Tags。比如:
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
當找到文章時返回此語句
<?php endwhile; else : ?>
當沒有找到文章時,返回此語句
<?php endif; ?>