使用 WP_Query 自定义文章显示


文章目录 隐藏

默认情况下,WordPress会自动将您的文章从最新到最旧排序。尽管访问者可以使用类别和标签搜索特定文章,但他们可能无法找到他们正在寻找的内容。要为每位访问者整理您的文章,使用WP_Query会很有帮助。

什么是 WP_Query?

WP_Query是一个PHP类,使您能够简化复杂的数据库请求。作为开发人员或网站所有者,它可以帮助您自定义超出默认主题的页面显示。以下是WP_Query的基本用法示例:

// WP QUERY
$query = new WP_Query([
    'post_type' => 'press-release',
    'posts_per_page' => 25,
    'category_name' => 'health',
]);

使用WP_Query,您可以创建自定义Loop来显示特定的文章内容。以下是一个基本的Loop示例:

<?php
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        // Display post content
    endwhile;
endif;
?>

通过插入WP_Query代码,您可以自定义Loop以仅显示某些文章:

<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

使用 WP_Query 的示例

  1. 某一分类的最新文章

    <?php
    $current_post_id = get_the_ID();
    $current_post_cats = get_the_category();
    $current_post_first_cat_id = $current_post_cats[0]->term_id;
    $args = array(
        'cat' => $current_post_first_cat_id,
        'post__not_in' => array($current_post_id)
    );
    $my_query = new WP_Query($args);
    ?>
  2. 本周发布的文章

    <?php
    $args = array(
        'date_query' => array(
            array(
                'year' => date('Y'),
                'week' => date('W'),
            ),
        ),
        'posts_per_page' => -1,
    );
    $posts = new WP_Query($args);
    ?>
  3. 按评论计数的热门文章

    <?php
    $args = array(
        'orderby' => 'comment_count'
    );
    $my_query = new WP_Query($args);
    ?>
  4. 同一作者和类别的文章

    <?php
    $args = array(
        'author_name' => 'john',
        'category_name' => 'fiction',
        'posts_per_page' => 3,
    );
    $posts = new WP_Query($args);
    ?>
  5. 作者的年发布文章

    <?php
    $current_year = date('Y');
    $args = array(
        'author' => 'john',
        'year' => $current_year
    );
    $my_query = new WP_Query($args);
    ?>
  6. 预览计划发布的文章

    <?php
    function tutsplus_show_drafts($show_excerpts = true) {
        $args = array(
            'post_status' => 'future',
            'nopaging' => true
        );
        $my_query = new WP_Query($args);
        if ($my_query->have_posts()) {
            $output = '<section class="pending-posts">';
            while ($my_query->have_posts()) {
                $my_query->the_post();
                $output .= '<div class="pending">';
                $output .= '<h3 class="pending-title">' . get_the_title() . '</h3>';
                if ($show_excerpts) {
                    $output .= '<div class="pending-excerpt">';
                    $output .= get_the_excerpt();
                    $output .= '</div>';
                }
                $output .= '</div>';
            }
            $output .= '</section>';
        } else {
            $output = '<section class="drafts-error">';
            $output .= '<p>' . __('Nothing found', 'tutsplus') . '</p>';
            $output .= '</section>';
        }
        wp_reset_postdata();
        return $output;
    }
    ?>

通过这些示例,您可以根据需要自定义WP_Query以实现不同的文章显示效


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至123@#-@12-3.com举报,一经查实,本站将立刻删除。

共有 30 条评论