<?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通过不同的标签获取不同的内容