似乎有三种主要方法可以使用其内置函数从Wordpress输出内容,推荐使用 WP_Query :
WP_Query
query_posts
get_posts
它们之间有什么区别? (我知道 WP_Query 是类,其他两个是方法) .
在同一页面上有多个循环的最简洁方法是什么,它们之间没有任何干扰?
我正在寻找你如何编程WP循环的例子;例如按类别输出2个单独的帖子列表,附件,元数据等
这是我到目前为止找到的最佳参考:
我已经使用了WP_Query和get_posts . 在我的一个侧边栏模板中,我使用以下循环来显示来自特定类别的帖子,方法是使用带有“category_to_load”键的自定义字段,其中包含类别slug或类别名称 . 真正的区别在于任何一种方法的实施 .
get_posts方法在我的一些模板中看起来如此:
<?php global $post; $blog_posts = get_posts($q_string); foreach($blog_posts as $post) : setup_postdata($post); ?> <div class="blog_post"> <div class="title"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <span class="date"><?php the_time('F j, Y'); ?> by <?php the_author(); ?></span> </div> <?php the_excerpt(); ?> </div> <?php endforeach; ?>
WP_Query实现如下所示:
$blog_posts = new WP_Query('showposts=15'); while ($blog_posts->have_posts()) : $blog_posts->the_post(); ?> <div <?php post_class() ?> id="post-<?php the_ID(); ?>" class="blog_post"> <div class="title"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> <span class="date"><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></span> </div> <div class="entry"> <?php the_content(); ?> </div> <p class="postmetadata"><?php the_tags('Tags: ', ', ', ''); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p> </div> <?php endwhile; ?>
主要区别在于您不必重置全局$ post变量,并且在使用WP_query时也不必通过在每个post对象上调用setup_postdata($ post)来设置post数据 . 您还可以在WP_Query函数上使用可爱的have_posts()函数,这在get_posts()中是不可用的 .
你不应该使用query_posts()函数,除非你真的想要,因为它修改了页面的主循环 . 见docs . 所以,如果你循环,那么你应该使用WP_Query .
那只是我的两分钱 . 我的最终建议,你的第一选择应该是WP_Query .
-克里斯
来自WP文档的get_posts:
get_posts()也可以获取query_posts()可以使用的参数,因为这两个函数现在在内部使用相同的数据库查询代码 .
这两个函数之间的唯一区别是get_posts返回一个包含post记录的数组,而query_posts将记录存储在查询对象中以供模板函数(has_posts,the_post等)检索 .
它们都使用WP_Query对象来执行查询 .
Wordpress docs中介绍了创建第二个循环 . 有一些链接用于多个循环的其他示例 . 你会注意到每个人都采用不同的方式,但他们都对结果感到满意 .
WP使用一个名为 $wp_query 的对象作为主循环 . 我们通常隐藏在 have_posts() 和 the_post() 背后的 have_posts() 只是 $wp_query->have_posts() 和 $wp_query->the_post() 的包装
$wp_query
have_posts()
the_post()
$wp_query->have_posts()
$wp_query->the_post()
如果要修改主循环,则应在循环前使用 query_posts() .
query_posts()
如果您想要另一个循环,可以在新循环之前使用 query_posts() 重新使用 $wp_query 对象 . 如果需要,这可以多次完成 .
如果由于某种原因你需要保持$ wp_query对象那么你应该使用 WP_Query . 当然,因为 have_posts() 和 the_post() 是 $wp_query 对象的包装器,所以不能将它们与 WP_Query 一起使用 . 你应该使用 $your_query_obj->have_posts() 即
$your_query_obj->have_posts()
$sidebar= WP_Query('category_name= sidebar'); while( $sidebar->have_posts() ): $sidebar->the_post(); the_title(); the_content(); endwhile;
WP_Query 可能比 query_posts() 更好的一个好例子是左侧边栏 . 由于侧边栏的代码循环可能会放在主循环的顶部, query_posts() 调用将更改 $wp_query 对象并更改主循环 . 在这种情况下,要在侧边栏代码中使用 query_posts() ,您还需要在主循环之前使用 query_posts() 来查询该循环的正确内容 .
所以在这种情况下使用WP_Query将保持$ wp_query,因此主循环不受影响 .
但同样,对于一个常见的案例场景 query_posts() 是查询内容的一种很好的方式:
query_posts('category_name=blog'); while( have_posts() ): the_post(); the_title(); the_content(); endwhile;
3 回答
我已经使用了WP_Query和get_posts . 在我的一个侧边栏模板中,我使用以下循环来显示来自特定类别的帖子,方法是使用带有“category_to_load”键的自定义字段,其中包含类别slug或类别名称 . 真正的区别在于任何一种方法的实施 .
get_posts方法在我的一些模板中看起来如此:
WP_Query实现如下所示:
主要区别在于您不必重置全局$ post变量,并且在使用WP_query时也不必通过在每个post对象上调用setup_postdata($ post)来设置post数据 . 您还可以在WP_Query函数上使用可爱的have_posts()函数,这在get_posts()中是不可用的 .
你不应该使用query_posts()函数,除非你真的想要,因为它修改了页面的主循环 . 见docs . 所以,如果你循环,那么你应该使用WP_Query .
那只是我的两分钱 . 我的最终建议,你的第一选择应该是WP_Query .
-克里斯
来自WP文档的get_posts:
这两个函数之间的唯一区别是get_posts返回一个包含post记录的数组,而query_posts将记录存储在查询对象中以供模板函数(has_posts,the_post等)检索 .
它们都使用WP_Query对象来执行查询 .
Wordpress docs中介绍了创建第二个循环 . 有一些链接用于多个循环的其他示例 . 你会注意到每个人都采用不同的方式,但他们都对结果感到满意 .
WP使用一个名为
$wp_query
的对象作为主循环 . 我们通常隐藏在have_posts()
和the_post()
背后的have_posts()
只是$wp_query->have_posts()
和$wp_query->the_post()
的包装如果要修改主循环,则应在循环前使用
query_posts()
.如果您想要另一个循环,可以在新循环之前使用
query_posts()
重新使用$wp_query
对象 . 如果需要,这可以多次完成 .如果由于某种原因你需要保持$ wp_query对象那么你应该使用
WP_Query
. 当然,因为have_posts()
和the_post()
是$wp_query
对象的包装器,所以不能将它们与WP_Query
一起使用 . 你应该使用$your_query_obj->have_posts()
即WP_Query
可能比query_posts()
更好的一个好例子是左侧边栏 . 由于侧边栏的代码循环可能会放在主循环的顶部,query_posts()
调用将更改$wp_query
对象并更改主循环 . 在这种情况下,要在侧边栏代码中使用query_posts()
,您还需要在主循环之前使用query_posts()
来查询该循环的正确内容 .所以在这种情况下使用WP_Query将保持$ wp_query,因此主循环不受影响 .
但同样,对于一个常见的案例场景
query_posts()
是查询内容的一种很好的方式: