wordpress循环WP Query在模版中调用示例

标准循环(loop)的结构 <?php // The Query $the_query = new WP_Query( $args ); // The Loop 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 { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); 示例: 将示例放到wordpress主题模版中调用,可得结果。 <?php //定义$args $args=array( 'post_type'=>'page', 'page_id'=>2 ); // The Query $the_query = new WP_Query( $args ); // The Loop //查询循环 if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); } ?> //输出 <h2><?php the_title();?></h2> <p><?php the_content();?></p> <a href="<?php the_permalink();?>">more details</a> <?php } else { // no...
阅读全文