开源夏令营(7)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值