2020-04-03 / 4693阅
WordPress会自动根据当前页面来循环文章。不同的页面使用同样的循环,得到的结果是不一样的。
在任何一个页面里使用循环,如果给循环传递任意参数,默认的循环将被打破
默认循环打破后,将输出参数控制查询的文章
例如:分页目录页面,默认情况下输出的是当前分类的文章,如果你传递一个控制数量的参数 showposts=5 给当前查询函数。那么循环将输出5篇文章,但这5篇文章不一定是当前分类下面的。为了达到效果。你还需要传入当前分类的别名或者ID
查询文章,我们一般使用以下方式:
$args = array( 'showposts'=>5, 'category__and'=>'5,8', //'category_name'=>'bai' ); query_posts($args); while (have_posts()):the_post(); //这里输出内容 endwhile; wp_reset_query();
同时,WordPress页支持字符串方式的查询
query_posts('showposts=5&category__and=5,8'); WordPress可以按照分类、ID、标签、时间、关键词、自定义字段、自定义分类法等多种方式查询文章
$args = array(
'post_type' => 'function',
'showposts' => 150,
's' => $_GET['keywords'],
'tax_query' => array($tax)
);
$query = new WP_Query( $args );
if ($query->have_posts()):
while ($query->have_posts()):$query->the_post();
?>
<li class="layui-col-xs6 layui-col-sm4 layui-col-md3">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php
endwhile;
else:
echo '<p style="line-height:50px;text-align:center">没有找到相关内容,请关注其它内容,尚未完成分类工作</p>';
endif; 阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228