<?php
$args=array(
'post_type'=>'page',//查找出所有頁面(多個結果集,複數)
'page_id'=>3//僅僅查詢id号為3的頁面,隻有一個結果,單數
);
// 執行個體化wp_query
$the_query = new WP_Query( $args );
// 開始循環
if ( $the_query->have_posts() ) {//如果找到了結果,便輸出以下内容
echo '<ul>';
while ( $the_query->have_posts() ) {//再次判斷是否有結果
$the_query->the_post();//不用問為什麼,每次都要寫這個;
echo '<li>' . get_the_title() . '</li>';//這裡開始輸出你想要的模闆标簽
}
echo '</ul>';
} else {
// 如果沒有找到任何結果,就輸出這個
}
wp_reset_postdata();//不用問為什麼,每次都記得寫就好
?>
wordpress的循環有兩種類型,一種是 自定義循環 ,一種是 預設循環 。
最簡單的例子
Standard Loop (Alternate)
<?php
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
WordPress通過不同的标簽擷取不同的内容