我连续有两个问题:
如何检查数组值,什么WP_Query
给我带来了什么
------------------- | 1 | 2 | ------------------- | 3 | 4 | ------------------- | 5 | -------------------但这两个问题我都不知道。
我连续有两个问题:
如何检查数组值,什么WP_Query
给我带来了什么
------------------- | 1 | 2 | ------------------- | 3 | 4 | ------------------- | 5 | -------------------但这两个问题我都不知道。
假设您从以下内容开始:
$custom_query_args = array(
\'cat\' => 10,
\'post_per_page\' => 5
);
$custom_query = new WP_Query($custom_query_args);
然后,要回答第一个问题,您可以通过以下方式查看返回的内容(用于开发/调试):var_dump( $custom_query );
要将查询输出为普通循环,只需使用have_posts()
和the_post()
查询类的成员函数:// Start loop
if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();
// Loop output goes here
// End loop
endwhile; endif;
// Restore $post data to the default query
wp_reset_postdata();
从查询中检索到的帖子存储在名为posts
. 您可以根据需要读取或修改该数组。例如,如果WP_Query
对象为$query
, 你可以做到$posts = $query->posts;
对于下一个问题,请不要使用while&;在你喜欢的地方展示它们
if(have_posts()) {
the_post();
the_title();
}
// some other code
if(have_posts()) {
the_post();
the_title();
}
the_post()
将当前帖子设置为对象帖子数组中的下一篇帖子。其余的只是纯php我有一个名为“tours”的自定义帖子类型和名为“tourtypes”的自定义分类法,因此我可以将其分类为“cultural tour”、“sports tour”等术语。我不是这个主题的原始作者,也不太懂分类学。但我想扩展它,这样用户就可以选择他们想要的旅游类别,而不是输出所有的“旅游”帖子。在主题模板分类tourtypes中。php这是显示有效术语标题的代码的一部分:<?php $terms = get_the_terms($post->ID, \'tourtypes\'); foreac