當前位置:首頁>WordPress建站>WordPress開發(fā)>結合 WP_Query 與主查詢(the Main Query)

結合 WP_Query 與主查詢(the Main Query)

到目前為止,在這個系列中,你已經學習了如何在你的主題或插件中使用 WP_Query 創(chuàng)建自定義查詢。在大多數情況下,你將使用 WP_Query 設置獨立于主查詢(the Main Query)的參數來查詢,但如果你希望包含主查詢的參數在你的參數中,你該怎么做?

注:由于時間精力有限,本教程沒辦法翻譯分享,希望朋友們可以加入我們,幫助我們進行翻譯,有小酬謝,有意者請聯系倡萌QQ 745722006(注明:教程翻譯)。

以下為原文:http://code.tutsplus.com/tutorials/combining-wp_query-with-the-main-query–cms-23209

So far in this series you’ve learned how to use WP_Query to create custom queries for use in your theme or plugins.

In most cases, you’ll use WP_Query with a completely new set of arguments which are separate from that in the main query, but what if you want to include the main query in your arguments?

Examples of when you might want to do this include:

  • on a category or taxonomy page, displaying only posts of one post type
  • on a category page, displaying posts with the current category and another category or a tag or taxonomy term
  • on a page for a post type, just displaying posts with certain metadata

I could go on—there are plenty of opportunities for combining the main query with your own custom query.

I’m going to demonstrate this with three examples: the first one will be a simple example with one loop; the second will use foreach to output multiple loops, one for each post type; and the third will output two post types on a category archive by using two separate queries.

Defining a Variable Based on the Main Query

However you’re going to combine your main query with WP_Query, you need to store the current query object in a way that makes it easy to use in your WP_Query arguments. The easiest way to do this is by assigning it to a variable.

You do this before defining your WP_Query arguments, like so:

$mainquery = get_queried_object();

The get_queried_object() function returns the currently queried object, whatever that may be. On a single post, it will just return the post object, while on an archive it will return the category, tag, term object or whatever object relates to the archive. It returns the ID of the queried object.

You can then use this $mainquery variable in your WP_Query arguments. Now let’s take a look at some examples.

Example 1: Displaying Only Posts of One Post Type on a Category Page

Let’s say your site has a custom post type added to it and you’ve enabled categories for that custom post type. On the category archive for each category, you don’t want to display posts: instead you want to display posts of your new post type—let’s call it product.

Your query might look something like this:

<?php
 
$mainquery = get_queried_object();
 
$args = array (
    'category_name' => $mainquery->slug,
    'post_type' => 'product'
);
 
// Custom query.
$query = new WP_Query( $args );
 
// Check that we have query results.
if ( $query->have_posts() ) {
 
    // Start looping over the query results.
    while ( $query->have_posts() ) {
 
        $query->the_post();
 
        // Contents of the queried post results go here.
 
    }
 
}
 
// Restore original post data.
wp_reset_postdata();
 
?>

Because the category_name parameter I’ve used above takes the category slug as its argument, you need to add ->slug after the variable to output the category slug.

This gives you a query which fetches posts of the product post type from the database with the currently queried category. You’d use it on the category.php page template.

Note: You could also achieve this result using the pre_get_posts hook to amend the main query, combined with a conditional function to check for category archives.

Example 2: Combining the Main Query With WP_Query and foreach to Output Multiple Loops

The next example will output all of the posts for the current category page, but instead of showing them all in one block it will separate them by post type.

This means you can sort your post types into blocks or columns on your page using CSS, or just separate them out into different lists.

To do this, you’d use the following code:

<?php
 
$mainquery = get_queried_object();
 
$post_types = get_post_types();
 
foreach ( $post_types as $post_type ) {
 
    $args = array(
        'category_name' => $mainquery->slug,
        'post_type' => $post_type
    );
 
    // Custom query.
    $query = new WP_Query( $args );
 
    // Check that we have query results.
    if ( $query->have_posts() ) {
 
        // Start looping over the query results.
        while ( $query->have_posts() ) {
 
            $query->the_post();
 
            // Contents of the queried post results go here.
 
        }
 
    }
 
    // Restore original post data.
    wp_reset_postdata();
 
}
 
?>

This uses the $mainquery variable we used before, but it also adds a $post_types variable to store all of the post types registered on the site, and a $post_type variable to store each individual post type in turn.

Example 3: Two Separate Queries for Two Post Types

The final example is similar to the second one, but separates out the post types into two separate queries, each with its own distinct loop. This gives you more control over what’s displayed for each, so you could display posts differently from products, maybe including a featured image for products or giving them a different layout.

Let’s say your site has the product post type registered, with categories enabled for it, and you’re also writing blog posts with the same categories. On each category archive page you want to display the most recent ten posts, and then you want to display a list of all products in the same category.

To do this, you’d use something like this code:

<?php
 
$mainquery = get_queried_object();
 
// First query arguments for posts.
$args = array (
    'category_name' => $mainquery->slug,
    'post_type' => 'post',
    'posts_per_page' => '10'
);
 
// Custom query.
$query = new WP_Query( $args );
 
// Check that we have query results.
if ( $query->have_posts() ) {
 
    // Start looping over the query results.
    while ( $query->have_posts() ) {
 
        $query->the_post();
 
        // Contents of the queried post results go here.
 
    }
 
}
 
// Restore original post data.
wp_reset_postdata();
 
// Second query arguments for products.
$args = array (
    'category_name' => $mainquery->slug,
    'post_type' => 'product',
    'posts_per_page' => '-1'
);
 
// Custom query.
$query = new WP_Query( $args );
 
// Check that we have query results.
if ( $query->have_posts() ) {
 
    // Start looping over the query results.
    while ( $query->have_posts() ) {
 
        $query->the_post();
 
        // Contents of the queried post results go here.
 
    }
 
}
 
// Restore original post data.
wp_reset_postdata();
 
?>

You’d then write each loop differently to output different data for each post type.

Summary

As you can see from the examples above, it’s possible to use WP_Query not only to create completely custom queries separate from the main query, but also to incorporate the currently queried object and create more powerful queries on archive pages.

The examples above can also be done with other archive types: for taxonomies, authors, dates and more. See if you can come up with more possibilities!

聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發(fā)布本站內容到任何網站、書籍等各類媒體平臺。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關注WordPress大學公眾號 WPDAXUE
WordPress開發(fā)

掌握 WP_Query:10個有用的例子

2016-5-8 8:10:43

WordPress開發(fā)

掌握 WP_User_Query

2016-5-9 7:30:00

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

郓城县| 永清县| 大同县| 富民县| 海门市| 邯郸市| 若羌县| 古浪县| 通许县| 大洼县| 吉木萨尔县| 麟游县| 聊城市| 井陉县| 铜鼓县| 马龙县| 广宁县| 广河县| 莎车县| 湖州市| 泸定县| 渝中区| 彩票| 庆安县| 洞头县| 上思县| 玉林市| 桂平市| 武冈市| 敦煌市| 元谋县| 贵州省| 隆昌县| 宝坻区| 句容市| 大关县| 那曲县| 汉沽区| 体育| 马尔康县| 陆良县|