【问题标题】:Wordpress WooCommerce: is there a way to view all products that are not in a category?Wordpress WooCommerce:有没有办法查看不在一个类别中的所有产品?
【发布时间】:2020-11-04 08:20:51
【问题描述】:

有些产品我只需要在美国展示。我有一个包含它们的标签为“美国”的类别,但是我需要将所有不在该类别中的产品放入一个名为“世界其他地区”的新产品中

有没有简单的方法可以做到这一点?

我正在尝试找到一种方法来展示美国的约 200 种产品(我的约 400 种产品商店),而无需进入每一种产品并标记或查找 ID。

我的计划是:

  1. 查找所有不在美国类别中的产品
  2. 将这些添加到新的“世界其他地区”类别中
  3. 对于美国的位置,隐藏“世界其他地区”类别
  4. 否则显示两个类别

如果有更好的解决方案,我会全力以赴!

【问题讨论】:

    标签: wordpress woocommerce categories product


    【解决方案1】:

    人们只是在没有任何反馈或理由的情况下放 -1 让我生气......

    Anw,只要您的产品属于其各自类别的一部分,以下内容就可以解决问题。我已经放了一堆选项,只是选择了最适合您的选项,您还可以查看此文档以了解有关 wordpress 查询https://developer.wordpress.org/reference/classes/wp_query/

    这只是一个通用循环,您可以在其中指定所需的帖子类型(此处产品有您使用 woocom)和所需的类别。

    如果它是自定义分类法,而不仅仅是插入到默认产品类别中的术语,那么您可能需要做更多的调整,但不是很多。希望这会对你有所帮助。

    <!-- Start loop here -->
    <?php
    /* For Choice 3 ONLY add the following:
    Warning: You need to use the category NAME
    $cat_id = get_cat_ID ( 'USA' ); */
    $query = new wp_query( array(
    'post_type'         => 'product',
    
    /* Choice 1 - Display product with category "USA" AND ALSO product with category "Rest of world"
    Using the separator ",".
    Warning: You need to use the category SLUG
    'category_name'     => 'usa,rest-of-the-world', */
    
    /* Choice 2 - Display product with category "USA" AND "Rest of world"
    Using the separator "+".
    Warning: You need to use the category SLUG
    'category_name'     => 'usa+rest-of-the-world', */
    
    /* Choice 3 - Display all products without category "USA"
    You could also use the category id
    'cat'     => '-' . $cat_id, */
    
    'post_status'       => 'publish',
    /* To display all products replace the post_per_page value by '-1'
    'posts_per_page'    => '-1', */
    'posts_per_page'    => '3',
    ) ); ?>
    <?php if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post(); ?>
    <!-- Start template here -->
    <div><a aria-label="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    <!-- End template here -->
    <?php endwhile; endif; ?>
    <!-- End loop here -->
    

    【讨论】:

    • 非常感谢,这真的很有帮助!原谅我的无知,我在哪里运行这个?我也同意 - 如果人们说为什么它被否决,那会有所帮助......
    • 粘贴到任何你想输出的地方。您可以创建一个新的页面模板或将其放入索引中。取决于你。
    • 太好了。然后我可以轻松地将这些产品批量添加到一个类别中吗?
    • 遗憾的是,我不这么认为...如果网站已经在线,通常您不会对其进行硬性更改。
    猜你喜欢
    • 2019-12-13
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2019-09-03
    • 2021-08-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多