如何不重复 wordpress loop/query?
How to not repeat wordpress loop/query?
这是我在 wordpress 中一直使用的一个坏习惯,我想改掉。当我应该一次获取所有内容时,我重复了 loop/query 代码。这是一个例子...
<?php
$args = array(
'p' => 9345, // id of a page, post, or custom type
'post_type' => 'front page content');
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
$args = array(
'p' => 9341, // id of a page, post, or custom type
'post_type' => 'front page content');
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content(); endwhile;
?>
如果我想先显示 id 9345
的内容,然后再显示 id 9341
的内容,如何在不复制 WP_Query 的情况下完成这一切?我知道 order_by,但如果我想专门对查询的结果进行排序怎么办?
您可以使用post__in
、Read more
<?php
$args = array(
'post__in' => array(9345, 9341),
'post_type' => 'page'
)
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
//DONT FORGET THIS
wp_reset_postdata();
?>
这是我在 wordpress 中一直使用的一个坏习惯,我想改掉。当我应该一次获取所有内容时,我重复了 loop/query 代码。这是一个例子...
<?php
$args = array(
'p' => 9345, // id of a page, post, or custom type
'post_type' => 'front page content');
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
$args = array(
'p' => 9341, // id of a page, post, or custom type
'post_type' => 'front page content');
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content(); endwhile;
?>
如果我想先显示 id 9345
的内容,然后再显示 id 9341
的内容,如何在不复制 WP_Query 的情况下完成这一切?我知道 order_by,但如果我想专门对查询的结果进行排序怎么办?
您可以使用post__in
、Read more
<?php
$args = array(
'post__in' => array(9345, 9341),
'post_type' => 'page'
)
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
//DONT FORGET THIS
wp_reset_postdata();
?>