推广特定分类的帖子
Promoting posts of a specific taxonomy
我有一个名为 "products" 的 CPT。它有不同的类别(汽车、手机、家居、美容等)
我想再创建一个并将其命名为 promoted
,并且我希望此类帖子列在其他帖子的顶部。
我现在有一个 pre_get_posts
钩子,如下所示:
add_action( 'pre_get_posts', function ( $q )
{
// Bail if we are on an admin page or if this is not the main query
if ( is_admin()
|| !$q->is_main_query()
)
return;
// Only targets the product-category tax pages
if ( $q->is_tax( 'product-category' ) ) {
$q->set( 'orderby', 'rand' );
}
if ( $q->is_home() ) {
// Set posts_per_page
$q->set( 'posts_per_page', get_option( 'zens_home_count' ) );
// Set custom post type
$q->set( 'post_type', 'products' );
// Set random ordering
$q->set( 'orderby', 'rand' );
}
});
我该怎么做?
我认为最好的方法是从主查询中完全删除 "vip" post,然后使用自定义查询或通过 the_posts
过滤器。
让我们从主页上的主查询和自定义 post 键入存档页面。我会将所有内容合并到一个操作中以将代码保存在一个地方
add_action( 'pre_get_posts', function ( \WP_Query $q )
{
// Bail if we are on an admin page or if this is not the main query
if ( is_admin() )
return;
if ( !$q->is_main_query() )
return;
// Only targets the product-category tax pages
if ( $q->is_tax( 'product-category' ) ) {
$q->set( 'orderby', 'rand' );
}
if ( $q->is_home() ) {
// Set posts_per_page
$q->set( 'posts_per_page', get_option( 'zens_home_count' ) );
// Set custom post type
$q->set( 'post_type', 'products' );
// Set random ordering
$q->set( 'orderby', 'rand' );
}
if ( $q->is_home()
|| $q->is_post_type_archive( 'products' )
|| $q->is_tax( 'product-category' )
) {
// Bail on the vip page
if ( !$q->is_tax( 'product-category', 'vip' ) ) { // Make sure about slug
$tax_query = [
[
'taxonomy' => 'product-category',
'terms' => 27,
'operator' => 'NOT IN'
]
];
$q->set( 'tax_query', $tax_query );
}
}
});
现在我们可以将 post 加回来
选项 1
通过 the_posts
过滤器
add_filter( 'the_posts', function ( $posts, \WP_Query $q )
{
if ( !is_admin() )
return $posts;
if ( !$q->is_main_query() )// Only target the main query
return $posts;
if ( !$q->is_paged() // Only target the first page
&& ( $q->is_home() // Only target the home page OR
|| $q->is_post_type_archive( 'products' ) // Only target the post type archive page OR
|| $q->is_tax( 'product-category' ) // Only target the taxonomy archive page AND
)
) {
// Bail on vip tax pages
if ( $q->is_tax( 'product-category', 'vip' ) )
return $posts;
// Lets get all vip posts
$args = [
'posts_per_page' => -1,
'post_type' => 'products',
'orderby' => 'rand', // Order these posts randomly
'tax_query' => [
[
'taxonomy' => 'product-category',
'terms' => 27
]
]
];
$vip_posts = get_posts( $args );
// Make sure we have vip posts, if not, we bail
if ( !$vip_posts )
return $posts;
// OK, we have vip posts, lets add them infront of the loop
$posts = array_merge( $vip_posts, $posts );
}
return $posts;
}, 10, 2 );
为此,为了保持 vip 广告独立,我们可以 运行 循环一次只显示 vip 广告,倒回循环然后再次运行 只显示普通广告
if ( have_posts() ) {
// Run the loop to display vip ads
while ( have_posts() ) {
the_post();
if ( has_term( 27, 'product-category' ) ) {
// Add your markup and stuff here for vip ads
}
} // end our vip ads loop
rewind_posts(); // Set the counter back to 1 so we can rerun the loop
while ( have_posts() ) {
the_post();
if ( !has_term( 27, 'product-category' ) ) {
// Your normal markup to display all ads
}
}
}
选项 2
通过自定义查询
$args = [
'posts_per_page' => -1,
'post_type' => 'products',
'orderby' => 'rand', // Order these posts randomly
'tax_query' => [
[
'taxonomy' => 'product-category',
'terms' => 27
]
]
];
$vip_posts = new WP_Query( $args );
// Run the loop
if ( $vip_posts->have_posts() ) {
while ( $vip_posts->have_posts() ) {
$vip_posts->the_post();
// Display what you want in your loop like
the_title();
echo '</br>';
}
wp_reset_postdata();
}
我有一个名为 "products" 的 CPT。它有不同的类别(汽车、手机、家居、美容等)
我想再创建一个并将其命名为 promoted
,并且我希望此类帖子列在其他帖子的顶部。
我现在有一个 pre_get_posts
钩子,如下所示:
add_action( 'pre_get_posts', function ( $q )
{
// Bail if we are on an admin page or if this is not the main query
if ( is_admin()
|| !$q->is_main_query()
)
return;
// Only targets the product-category tax pages
if ( $q->is_tax( 'product-category' ) ) {
$q->set( 'orderby', 'rand' );
}
if ( $q->is_home() ) {
// Set posts_per_page
$q->set( 'posts_per_page', get_option( 'zens_home_count' ) );
// Set custom post type
$q->set( 'post_type', 'products' );
// Set random ordering
$q->set( 'orderby', 'rand' );
}
});
我该怎么做?
我认为最好的方法是从主查询中完全删除 "vip" post,然后使用自定义查询或通过 the_posts
过滤器。
让我们从主页上的主查询和自定义 post 键入存档页面。我会将所有内容合并到一个操作中以将代码保存在一个地方
add_action( 'pre_get_posts', function ( \WP_Query $q )
{
// Bail if we are on an admin page or if this is not the main query
if ( is_admin() )
return;
if ( !$q->is_main_query() )
return;
// Only targets the product-category tax pages
if ( $q->is_tax( 'product-category' ) ) {
$q->set( 'orderby', 'rand' );
}
if ( $q->is_home() ) {
// Set posts_per_page
$q->set( 'posts_per_page', get_option( 'zens_home_count' ) );
// Set custom post type
$q->set( 'post_type', 'products' );
// Set random ordering
$q->set( 'orderby', 'rand' );
}
if ( $q->is_home()
|| $q->is_post_type_archive( 'products' )
|| $q->is_tax( 'product-category' )
) {
// Bail on the vip page
if ( !$q->is_tax( 'product-category', 'vip' ) ) { // Make sure about slug
$tax_query = [
[
'taxonomy' => 'product-category',
'terms' => 27,
'operator' => 'NOT IN'
]
];
$q->set( 'tax_query', $tax_query );
}
}
});
现在我们可以将 post 加回来
选项 1
通过 the_posts
过滤器
add_filter( 'the_posts', function ( $posts, \WP_Query $q )
{
if ( !is_admin() )
return $posts;
if ( !$q->is_main_query() )// Only target the main query
return $posts;
if ( !$q->is_paged() // Only target the first page
&& ( $q->is_home() // Only target the home page OR
|| $q->is_post_type_archive( 'products' ) // Only target the post type archive page OR
|| $q->is_tax( 'product-category' ) // Only target the taxonomy archive page AND
)
) {
// Bail on vip tax pages
if ( $q->is_tax( 'product-category', 'vip' ) )
return $posts;
// Lets get all vip posts
$args = [
'posts_per_page' => -1,
'post_type' => 'products',
'orderby' => 'rand', // Order these posts randomly
'tax_query' => [
[
'taxonomy' => 'product-category',
'terms' => 27
]
]
];
$vip_posts = get_posts( $args );
// Make sure we have vip posts, if not, we bail
if ( !$vip_posts )
return $posts;
// OK, we have vip posts, lets add them infront of the loop
$posts = array_merge( $vip_posts, $posts );
}
return $posts;
}, 10, 2 );
为此,为了保持 vip 广告独立,我们可以 运行 循环一次只显示 vip 广告,倒回循环然后再次运行 只显示普通广告
if ( have_posts() ) {
// Run the loop to display vip ads
while ( have_posts() ) {
the_post();
if ( has_term( 27, 'product-category' ) ) {
// Add your markup and stuff here for vip ads
}
} // end our vip ads loop
rewind_posts(); // Set the counter back to 1 so we can rerun the loop
while ( have_posts() ) {
the_post();
if ( !has_term( 27, 'product-category' ) ) {
// Your normal markup to display all ads
}
}
}
选项 2
通过自定义查询
$args = [
'posts_per_page' => -1,
'post_type' => 'products',
'orderby' => 'rand', // Order these posts randomly
'tax_query' => [
[
'taxonomy' => 'product-category',
'terms' => 27
]
]
];
$vip_posts = new WP_Query( $args );
// Run the loop
if ( $vip_posts->have_posts() ) {
while ( $vip_posts->have_posts() ) {
$vip_posts->the_post();
// Display what you want in your loop like
the_title();
echo '</br>';
}
wp_reset_postdata();
}