宣传特定分类的职位

时间:2016-02-09 15:55:36

标签: php wordpress

我有一个名为"产品"的CPT。它有不同的类别(汽车,手机,家庭,美容等)

我想创建另一个并将其命名为//td[@id='tvMain_item_20_expcol']/img[@src='Images/Tree/tplus.gif'] ,我希望将此类别的帖子列在其他人的首位。

我现在有一个promoted钩子,如下所示:

pre_get_posts

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我认为最好的方法是从主查询中完全删除“vip”帖子,然后使用自定义查询或通过the_posts过滤器将其添加回循环顶部。

让我们从主页上的主查询和自定义帖子类型存档页面中删除“vip”帖子( ID 27 )。我将所有内容合并为一个动作以将代码保存在一个地方

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 );
        }
    }
}); 

现在我们可以添加这些帖子

选项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广告的独立性,我们可以运行一次循环以仅展示贵宾广告,重绕循环然后再次重新运行以仅展示普通广告

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();
}