Define your own WordPress loop search

本文详细介绍了如何使用WP_Query类来替代WordPress的query_posts函数,实现自定义循环,避免了可能引发的插件冲突和条件标签失效问题。通过实例展示了创建最近文章列表的方法,并讨论了使用Pagination的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

We all know what the<wbr></wbr>WordPressLoop<wbr></wbr>is right? If not,there are many great tutorials around the web that<wbr></wbr>explain theWordPress Loop.

One of the easiest ways to navigate and manipulate the loop is touse the function called<wbr></wbr>query_posts.<wbr></wbr>NathanRice<wbr></wbr>callsit<wbr></wbr>aWordPress developers best friend.

When you use<wbr></wbr>query_posts,however, you risk the following:

  • Potential to interfere with plugins which make use of theLoop.
  • Potential to invalidate WordPress conditional tags.
  • Having to deal with resetting, rewinding, offsetting…

I say skip<wbr></wbr>query_posts.In a way you’ll still be using it, but the better (and sometimeseasier) technique is to instantiate your own<wbr></wbr>WP_Query<wbr></wbr>objectand create your own loop.

Creating Your Own Loop With WP_Query

The first step is to instantiate your own variable using theWP_Query class.

What we’ll be doing in this example is creating a common feature onblogs, which is to display a list of the recent articles.

<wbr></wbr>

<?php
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=5');
?>

<wbr></wbr>

All I’ve done in the above code is defined a variablenamed<wbr></wbr>recentPosts<wbr></wbr>andinstantiated an instance of<wbr></wbr>WP_Query.

I then used a method of<wbr></wbr>WP_Query<wbr></wbr>tostart a query (pretty much the same thing asusingquery_posts). You even use the<wbr></wbr>same usageparameters<wbr></wbr>as<wbr></wbr>query_posts.

Now it’s time to start our own loop:

<wbr></wbr>

<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
   <!-- do some stuff here -->
<?php endwhile; ?>

<wbr></wbr>

Notice the use of the<wbr></wbr>recentPosts<wbr></wbr>variableto start the loop. We utilize two methods of WP_Query, whichis<wbr></wbr>have_posts<wbr></wbr>and<wbr></wbr>the_post.You can read more about those two methods on my article<wbr></wbr>GlobalVariables and the WordPress Loop.

The beauty of this is that once you are inside your own loop, youcan use the<wbr></wbr>standardpost tags.

The Full Code

Here’s the full code for showing the last five recent posts usingyour own loop:

<wbr></wbr>

<h3>Recent Articles</h3>
<ul>
<?php
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=5');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>

<wbr></wbr>

Update: Using Pagination

This comment is from<wbr></wbr>AaronHarun<wbr></wbr>-

@Ron and Monika

If you use the query:

<wbr></wbr>

$recentPosts->query('showposts=5'.'&paged='.$paged);

<wbr></wbr>

it will automatically page the wury based on the page numberpassed
through the url eg “/page/4″

However, this doesn’t work with the“post_nav_link” function because it
only checks the $wp_query<wbr></wbr>variable. To getaround this you have to
trick the function by switching$wp_query<wbr></wbr>on it. Add thefirst block
before your custom loop and the second after to achieve thiseffect.

<wbr></wbr>

<?php //query_posts('paged='.$paged);
$temp = $wp_query;
$wp_query= null;
   $wp_query = new WP_Query();
   $wp_query->query('showposts=5'.'&paged='.$paged);
?>

<wbr></wbr>

<wbr></wbr>

<?php $wp_query = null; $wp_query = $temp;?>

<wbr></wbr>

Thanks Aaron for the contribution.

Conclusion

Defining your own loop using WP_Query is an easy way to run yourown custom queries without interfering with the default Loop. It’salso a great way to run multiple loops that are completelyindependent of each other.

WordPress Resources mentioned:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值