您可以使用不同的技术来实现这一点-
其中一个是get_posts()
作用您可以在其中传递参数,并获得特定帖子的数组。那么你必须运行foreach
循环以根据需要显示这些帖子。
您还可以使用WP_Query()
. 它还需要如下参数get_posts()
但是在每次查询之后,您需要使用wp_reset_query()
.
第一种情况下-
//getting header with get_header() - not important
...some code...
//and here I want to add some specific post from specific category(ONLY this specific post)
$args_1 = [
\'category__in\' => 1 // category id
];
$posts_1 = get_posts( $args_1 );
foreach ($posts_1 as $post_1){
// show posts as you want
}
//another specific post from specific category(only this specific post again).
$args_2 = [
\'category__in\' => 2 // category id
];
$posts_2 = get_posts( $args_2 );
foreach ($posts_2 as $post_2){
// show posts as you want
}
//and so on
//getting footer with get_footer() - not important
第二种情况-
//getting header with get_header() - not important
...some code...
//and here I want to add some specific post from specific category(ONLY this specific post)
$args_1 = [
\'category__in\' => 1 // category id
];
$posts_1 = new WP_Query( $args_1 );
while ( $posts_1->have_posts() ) : $posts_1->the_post();
get_template_part( \'template-parts/content\', \'page\' );
// If comments are open or we have at least one comment, load up the comment template.
endwhile; // End of the loop.
wp_reset_query();
//another specific post from specific category(only this specific post again).
$args_2 = [
\'category__in\' => 2 // category id
];
$posts_2 = new WP_Query( $args_2 );
while ( $posts_2->have_posts() ) : $posts_2->the_post();
get_template_part( \'template-parts/content\', \'page\' );
// If comments are open or we have at least one comment, load up the comment template.
endwhile; // End of the loop.
wp_reset_query();
//and so on
//getting footer with get_footer() - not important