在同一页面上的 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;
}

只需在新的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;
}

相关内容

最新更新