在 WordPress 中,我是否需要多个 WP_Query 对象才能在首页显示多个类别和自定义类型的帖子?

Krn*_*ell 5 php wordpress

首先,英语不是我的母语,而且我对编程、PHP 和 WordPress 还很陌生,所以请耐心回答并且非常简单:)

我正在为显示视频、文章、电影评论、图片库、活动和购物区的多功能博客创建自定义主题,访问者可以在其中购买和下载一些电子书。

在首页中,我想显示最后条目的一般摘要,包括:

  • 3 个不同的博客文章类别(文章、电影评论、视频)中的每一个的最后 3 个条目;
  • 接下来的 3 个传入事件
  • 最近 3 个过去的事件
  • 最近发布的 3 个画廊
  • 最近 3 本书可下载的电子书

我的问题是:我是否需要调用 7 个不同的 WP_Query 对象才能使其工作,每个类别/post_type 一个,或者是否有更好的方法来完成它?

也许使用pre_get_posts 钩子,或者一些条件标签,比如get_post_type()is_category()?我尝试了这些方法和其他一些方法,但由于缺乏编程技能,我一无所获。任何建议都非常受欢迎。

(添加了我实际使用的代码的相关部分)

<!-- // start Front Page contents - BLOG-->
    <!-- start tab dei post -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'post' ) ); ?>
            <!--// seleziona risultati per categoria - POST -->
            // here is included the Loop for - POST
        <?php wp_reset_postdata(); ?>
    <!-- end tab dei post -->
    <!-- start tab rece -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'rece' ) ); ?>
            <!--// seleziona risultati per categoria - RECE -->
            // here is included the Loop for - REVIEWS
        <?php wp_reset_postdata(); ?>
    <!-- end tab rece -->
    <!-- start tab video -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'vide' ) ); ?>
            <!--// seleziona risultati per categoria - VIDE -->
            // here is included the Loop for - VIDEOS
        <?php wp_reset_postdata(); ?>
    <!-- end tab video -->
<!-- // end Front Page contents - BLOG-->
<!-- // start Front Page contents - GALL-->
    <!-- // Define our WP Query Parameters-->
    <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'gall' ) ); ?>
    <!--// Start our WP Query-->
        // here is included the Loop for - GALLERIES
    <?php wp_reset_postdata(); ?>
<!-- // end Front Page contents - GALL-->
<!-- // start Front Page contents - BOOK-->
    <!-- // Define our WP Query Parameters-->
    <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','post_type' => 'download' ) ); ?>
    <!--// seleziona risultati per categoria - BOOK -->
        // here is included the Loop for - DOWNLOADS
    <?php wp_reset_postdata(); ?>
<!-- // end Front Page contents - BOOK-->
<!-- // start Front Page contents - EVEN-->
    <!-- start tab EVENTI FUTURI -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','post_type' => 'event','scope' => 'future' ) ); ?>
        <!--// Start our WP Query-->
            // here is included the Loop for - Incoming Events
        <?php wp_reset_postdata(); ?>
    <!-- end tab EVENTI FUTURI -->
    <!-- start tab EVENTI PASSATI -->
        <!-- // Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( array( 'posts_per_page' => '3','post_type' => 'event','scope' => 'past' ) ); ?>
        <!--// Start our WP Query-->
            // here is included the Loop for - Past Events
        <?php wp_reset_postdata(); ?>
    <!-- end tab EVENTI PASSATI -->
<!-- // end Front Page contents - EVEN-->
Run Code Online (Sandbox Code Playgroud)

小智 1

是的,如果所有内容属于不同的帖子类型,您必须以不同的方式查询它们。

在博客类别的情况下,您可以执行以下操作:

$pp = 3;
$cats = ("post","rece","gall",...);

foreach($cats as $cat){
    $the_query = new WP_Query( array( 'posts_per_page' => $pp,'category_name' => $cat ) );
    //Loop in here
}
Run Code Online (Sandbox Code Playgroud)

这样您可以节省行数和时间,此外,如果帖子类型查询是相同的帖子类型,您可以应用相同的逻辑。

让我知道这是否适合您!