多种 post 类型,但只为一种类型设置偏移量
Multiple post types but set the offset only for one
我正在显示多种 post 类型,但我想为其中一种 post 类型设置偏移量,但我该怎么做?
$args = array(
'post_type' => array('wins', 'memes'),
'posts_per_page' => '5',
//'offset' => '1', (with this i set the offset for both but i only want to set it for one of them.)
'post_status' => 'publish'
);
您可以收集不需要的 ID。如果您没有 post 类型所需的具有偏移量的特定顺序,并且只是从该类型中查找最近的 5 个 post。您可以执行以下操作:
<?php
$posts_to_exclude = array();
$args = array(
'post_type' => 'wins', // post type you want to offset
'numberposts' => 5 // the default is 5, but you can add for good measure
);
$posts = get_posts( $args );
if ($posts) {
foreach ($posts as $post) {
$posts_to_exclude[] = $post->ID;
}
}
$args = array(
'post_type' => array('wins', 'memes'),
'posts_per_page' => '5',
'post__not_in' => $posts_to_exclude,
'post_status' => 'publish'
);
new WP_Query( $args );
// Do more stuff....
我正在显示多种 post 类型,但我想为其中一种 post 类型设置偏移量,但我该怎么做?
$args = array(
'post_type' => array('wins', 'memes'),
'posts_per_page' => '5',
//'offset' => '1', (with this i set the offset for both but i only want to set it for one of them.)
'post_status' => 'publish'
);
您可以收集不需要的 ID。如果您没有 post 类型所需的具有偏移量的特定顺序,并且只是从该类型中查找最近的 5 个 post。您可以执行以下操作:
<?php
$posts_to_exclude = array();
$args = array(
'post_type' => 'wins', // post type you want to offset
'numberposts' => 5 // the default is 5, but you can add for good measure
);
$posts = get_posts( $args );
if ($posts) {
foreach ($posts as $post) {
$posts_to_exclude[] = $post->ID;
}
}
$args = array(
'post_type' => array('wins', 'memes'),
'posts_per_page' => '5',
'post__not_in' => $posts_to_exclude,
'post_status' => 'publish'
);
new WP_Query( $args );
// Do more stuff....