掌握WP_Query:使用循环

正如我在本系列的简介中概述的那样, WP_Query类具有四个主要元素:

  • 查询的参数,使用将在本系列中详细介绍的参数
  • 查询本身
  • 循环,将输出帖子内容,标题或您要显示的任何内容
  • 完成:关闭if和while标签并重置发布数据

在本教程中,我将向您展示如何在WP_Query使用循环,包括构造循环的两种主要方法以及如何使用多个循环。

环路适合的位置

没有循环 ,页面上将不会显示任何内容。 在WordPress使用您定义的参数运行查询之后,需要告知它从所获取的数据中输出什么。 这是循环进入的地方。

因此,循环出现在查询之后,它使用三个标签:

  • if( $query->have_posts() )检查是否有任何帖子。
  • while( $query->have_posts() )对每个帖子重复循环,只要有要检索的帖子。
  • $query->the_post()访问该特定帖子。

因此,这是循环适合WP_Query类的地方:

<?php

$args = array(
    // Arguments for your query.
);

// 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();

?>

运行循环后,剩下要做的就是使用wp_reset_postdata()整理整齐的东西。

循环结构

循环的结构方式取决于您要从帖子中显示哪些数据。 这是一个示例循环,输出帖子标题,特色图片和摘录。 您可以在存档页面上使用这样的循环。

<?php

$args = array(
    // Arguments for your query.
);

// 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();

        ?>

        <article id="post-<?php the_ID(); ?>" <?php post_class( 'left' ); ?>>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php post_thumbnail( 'thumbnail' );?>
            </a>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_title(); ?>
            </a>
            <?php the_excerpt(); ?>
        </article>

        <?php

    }

}

// Restore original post data.
wp_reset_postdata();

?>

该循环完全显示了我上面描述的内容:精选图片,标题和摘录。

进一步循环:检查内容

但是有时您可能想要在帖子列表之前添加标题,或者可能希望将它们全部包含在包含元素中。 如果仅在循环之前添加此内容,则无论查询是否实际上返回了任何数据,都将输出该内容,这意味着您可能在标题下没有任何内容,或者有不必要的标记。

通过将封闭元素或标题放在if标记内,可以轻松解决此问题:

<?php

$args = array(
    // Arguments for your query.
);

// Custom query.
$query = new WP_Query( $args );

// Check that we have query results.
if ( $query->have_posts() ) {

    
    echo '<section class="clear">';
        echo '<h2>' . __( 'Heading', 'tutsplus' ) . '</h2>';
    
        // Start looping over the query results.
        while ( $query->have_posts() ) {
    
            $query->the_post();
    
            ?>
    
            <article id="post-<?php the_ID(); ?>" <?php post_class( 'left' ); ?>>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php post_thumbnail( 'thumbnail' );?>
                </a>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                </a>
                <?php the_excerpt(); ?>
            </article>
    
            <?php
    
        }
    
    echo '</section>';

}

// Restore original post data.
wp_reset_postdata();

?>

在这里,您可以看到我检查了查询是否检索到了任何帖子,并且是否打开了包含元素并添加了标题。

如果要将查询结果作为列表输出,这也很有用。 假设我要创建给定类别中所有帖子的列表。 ul元素不在我的循环中,因为它与一个特定的帖子无关,但是我只想在有帖子的情况下将其输出。 所以我用这个:

<?php

$args = array(
    'category_name' => 'category-slug',
    'post_type' => 'post'
);

// Custom query.
$query = new WP_Query( $args );

// Check that we have query results.
if ( $query->have_posts() ) {

    echo '<ul class="category posts">';

        // Start looping over the query results.
        while ( $query->have_posts() ) {

            $query->the_post();

            ?>

            <li <?php post_class( 'left' ); ?>>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                </a>
            </li>

            <?php
    
        }

    echo '</ul>';

}

// Restore original post data.
wp_reset_postdata();

?>

这将检查查询是否获取了任何帖子,如果是,它将打开ul元素,然后运行循环。

运行额外的循环

重要的是要意识到,尽管可以使用WP_Query运行多个循环,但是您必须重置发布数据并启动WP_Query的第二个实例来执行此操作。 这是因为每个循环都将基于不同的参数输出数据。

此示例显示第一篇文章的摘录和特色图片,然后仅显示每篇后续文章的标题:

<?php

// First query arguments.
$args1 = array(
    'post_type' => 'post',
    'posts_per_page' => '1'
);

// First custom query.
$query1 = new WP_Query( $args1 );

// Check that we have query results.
if ( $query1->have_posts() ) {

    // Start looping over the query results.
    while ( $query1->have_posts() ) {

        $query1->the_post();

        ?>

        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php post_thumbnail( 'thumbnail' );?>
            </a>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_title(); ?>
            </a>
            <?php the_excerpt(); ?>
        </article>

        <?php

    }

}

// Restore original post data.
wp_reset_postdata();

// Second query arguments.
$args2 = array(
    'offset' => '1',
    'post_type' => 'post'
);

// Second custom query.
$query2 = new WP_Query( $args2 );

// Check that we have query results.
if ( $query2->have_posts() ) {

    echo '<ul class="more-posts">';
    
        // Start looping over the query results.
        while ( $query2->have_posts() ) {

            $query2->the_post();
    
            ?>

            <li <?php post_class(); ?>>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                </a>
            </li>
    
            <?php
    
        }
    
    echo '</ul>';

}

// Restore original post data.
wp_reset_postdata();

?>

我在这里使用了两个关键参数:

  • 与第一个查询一起使用的'posts_per_page' => '1'仅输出最新的帖子。
  • 与第二个查询一起使用的'offset' = '1'跳过第一条帖子,确保在下面的列表中不再重复。
  • 从上面的代码中可以看到,每个查询的循环略有不同。 第一个输出特色图像,标题和摘录,第二个输出查询查询中是否有帖子,如果有,则打开ul元素,并将每个帖子标题括在li元素中,并链接到其页面。

您还会注意到,在两个循环之后我都使用了wp_reset_postdata() 。 如果我没有这样做,第二个循环仍然会从第一个循环输出数据。

摘要

没有循环, WP_Query并不会做很多事情。 循环是用于显示WordPress根据查询参数从数据库获取的数据的代码。

如我所展示的,循环上有一些变化。 一个简单的循环将按照您在查询参数中指定的顺序(或默认情况下按日期降序)输出所有帖子。 如果将if( $query->have_posts() )while( $query->have_posts() ) ,则可以在循环外插入其他标记,但while( $query->have_posts() )查询已返回数据。 最后,通过指定备用参数并在每个循环之后使用wp_reset_postdata() ,可以WP_Query使用WP_Query在页面上创建多个循环。

翻译自: https://code.tutsplus.com/tutorials/mastering-wp_query-using-the-loop--cms-23031

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值