脚本宝典收集整理的这篇文章主要介绍了php – 创建自己的WordPress循环的最佳方法是什么?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
> WP_Query
> query_posts
> get_posts
它们之间有什么区别? (我知道WP_Query是类,其他两个是方法).
在同一页面上拥有多个循环的最简洁方法是什么,它们之间没有任何干扰?
我正在寻找你如何编程WP循环的例子;
例如按类别输出2个单独的帖子列表,附件,元数据等
这是我到目前为止找到的最佳参考:
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: ',','<br />'); ?> Posted in <?PHP the_category(',') ?> | <?PHP edit_post_link('Edit','',' | '); ?> <?PHP comments_popup_link('No Comments &#187;','1 Comment »','% Comments »'); ?></p> </div> <?PHP endwhile; ?>
主要区别在于您不必重置全局$post变量,并且在使用WP_query时也不必通过在每个post对象上调用setup_postdata($post)来设置POST数据.您还可以在WP_Query函数上使用可爱的have_posts()函数,这在get_posts()中是不可用的.
你不应该使用query_posts()函数,除非你真的想要,因为它修改了页面的主循环.请参阅docs.因此,如果您要构建一个特殊页面来显示您的博客,那么调用query_posts可能会弄乱页面的循环,因此您应该使用WP_Query.
那只是我的两分钱.我的最终建议,你的第一选择应该是WP_Query.
-克里斯
以上是脚本宝典为你收集整理的php – 创建自己的WordPress循环的最佳方法是什么?全部内容,希望文章能够帮你解决php – 创建自己的WordPress循环的最佳方法是什么?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。