在同一页面的wodrpess中使用两个循环:第一个应该“内置”到静态页面,第二个应该使用自定义参数

我的页面带有简单循环。它仅显示静态页面的内容。 但是,在某些页面的底部,我需要添加second loop来查找某些category中的所有帖子。当我添加第二个循环时,我的页面变为空白-第一个循环和第二个循环均不起作用。我是Wordpress开发的新手,因此我缺少一些东西。

这是我的page.php上的简化代码:

<?php

// First loop - 'auto' loop. It should just show given page content.
while ( have_posts() ) : the_post();
    get_template_part( 'template-parts/content','page' );
endwhile; // End of the loop.

// Second loop - it would contain custom arguments
$category_id = get_categor_for_page(get_the_ID());
if (isset($category_id)) {
    $post_query = new WP_Query( array( 'cat' => $category_id,'post_type' => 'post');
    while ( $post_query->have_posts() ) : $my_query->the_post();
        // show posts from given category in carousel; for now just show in a list
        get_template_part( 'template-parts/content','post' );
    endwhile;
}
imauzhw 回答:在同一页面的wodrpess中使用两个循环:第一个应该“内置”到静态页面,第二个应该使用自定义参数

只需在新的WP_Query和$ post_query-> the_post()而不是$ my_query-> the_post()中添加右括号

  <?php

    // First loop - 'auto' loop. It should just show given page content.
    while ( have_posts() ) : the_post();
        get_template_part( 'template-parts/content','page' );
    endwhile; // End of the loop.

    // Second loop - it would contain custom arguments
    $category_id = get_categor_for_page(get_the_ID());
    if (isset($category_id)) {
        $post_query = new WP_Query( array( 'cat' => $category_id,'post_type' => 'post'));
        while ( $post_query->have_posts() ) : $post_query->the_post();
            // show posts from given category in carousel; for now just show in a list
            get_template_part( 'template-parts/content','post' );
        endwhile;
    }
本文链接:https://www.f2er.com/3145560.html

大家都在问