WooCommerce相关产品按2个分类

时间:2018-06-27 作者:Mostafa AHmadi

我需要按分类法更改相关产品,如果分类法没有足够的产品,则显示其他分类法的其余部分。

我有2个分类学;product_tagproduct_cat. 有时产品product_tag 不超过4个,所以我需要使用另一个分类法来实现相关产品中的4个产品。

所以我需要4个相关产品,如果product\\u标签上都有,那么什么都没有,如果没有,请使用product\\u cat来完成4个产品。

任何帮助都将是巨大的。

<?php
    // get the custom post type\'s taxonomy terms
    $related_category = wp_get_object_terms( $post->ID, \'product_cat\', array(\'fields\' => \'ids\') );
    $related_tag = wp_get_object_terms( $post->ID, \'product_tag\', array(\'fields\' => \'ids\') );
    // arguments
    $args = array(
    \'post_type\' => \'product\',
    \'post_status\' => \'publish\',
    \'posts_per_page\' => 4, // you may edit this number
    \'orderby\' => \'rand\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'product_tag\',
            \'field\' => \'id\',
            \'terms\' => $related_tag
        )
    ),
    \'post__not_in\' => array ($post->ID),
    );
    $related_items = new WP_Query( $args );
    // loop over query
    if ($related_items->have_posts()) :
    while ( $related_items->have_posts() ) : $related_items->the_post();
    ?>
    <div class="related_item">
      <div class="Related_image">
        <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(\'thumbnail\'); ?></a>
      </div>
        <div class="Related_title">
          <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><h2>

        </div>
      </div>
    <?php
    endwhile;
    endif;
    // Reset Post Data
    wp_reset_postdata();
    ?>

1 个回复
SO网友:Mostafa AHmadi

我想我做到了。因此,如果此代码错误或可以简化,请更正它。我想要5个产品结果。所以所有这些都应该来自product_tag 如果可用。如果不使用product_cat. 所以我在这里组合了2个参数:

<?php
        // get the custom post type\'s taxonomy terms
        $related_tag = wp_get_object_terms( $post->ID, \'product_tag\', array(\'fields\' => \'ids\'));
        $related_cat = wp_get_object_terms( $post->ID, \'product_cat\', array(\'fields\' => \'ids\'));


        // arguments 1
        $args1 = array(
        \'post_type\' => \'product\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => 5,
        \'orderby\' => \'\',

        \'tax_query\' => array(

            array(

                \'taxonomy\'     => \'product_tag\',
                \'field\'        => \'id\',
                \'terms\'        =>  $related_tag,
            ),

        ),
        \'post__not_in\' => array ($post->ID),
         );
        // arguments 2
        $args2 = array(
        \'post_type\' => \'product\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => 5,
        \'orderby\' => \'rand\',

        \'tax_query\' => array(

            array(

                \'taxonomy\'     => \'product_cat\',
                \'field\'        => \'id\',
                \'terms\'        =>  $related_cat,
            ),

        ),
        \'post__not_in\' => array ($post->ID),

        );

        $related_items1 = new WP_Query( $args1 );
        $related_items2 = new WP_Query( $args2 );
        $related_items = new WP_Query();
        $related_items->posts = array_merge( $related_items1->posts, $related_items2->posts );

        $related_items3->post_count=5 - $related_items1->post_count;

        $related_items->post_count = $related_items1->post_count + $related_items3->post_count;


        // loop over query
        if ($related_items->have_posts()) :
        while ( $related_items->have_posts() ) : $related_items->the_post();
        ?>
        <div class="related_item">
          <div class="Related_image">
            <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(\'thumbnail\'); ?></a>
          </div>
            <div class="Related_title">
              <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><h2>

            </div>
          </div>
        <?php
        endwhile;
        endif;
        // Reset Post Data
        wp_reset_postdata();
        ?>

结束

相关推荐

创建自定义php页面并将其加载到特定的插件

我和一个客户有一个痛苦的问题。他们需要从远程端点将提要加载到符合WordPress模板设计的页面中。。他们还希望在特定URL处显示页面例如myclient.com/we-want-it-here 我创建了加载提要的页面,可以包含页眉和页脚,,<?php define(\'WP_USE_THEMES\', false); require(\'path/to/my-wordpress-dir/wp-blog-header.php\'); get_