使用术语而不是分类法显示自定义 post 类型

Displaying custom post type using terms instead of taxonomy

我目前正在构建一个相当复杂的站点,需要一种方法来使用各种方式显示所有自定义 post 类型。

我的自定义 post 目前在这个结构中布局和定义:

自定义 post 类型: 报价

分类法:

目前我只能显示以下内容:

  1. 特定品牌显示所有优惠类型
  2. 特定品牌显示特定商品类型

我也想要它所以我可以通过显示特定报价类型显示所有品牌来显示以下情况。

我假设实现此目的的正确方法是为简码 'brand' 属性创建一个数组,以便我可以列出 'brand 1'、'brand 2' 等?但是,我不确定这将如何实现。我无法获得使用数组作为类型的短代码,所以它目前是所有类型或 1。

我使用的简码是 [offers brand='brand-1' type='offer-type-1']

希望上面的内容有意义,我的主题 functions.php 文件中的当前代码如下:

            function register_shortcodes() {
                add_shortcode( 'offers', 'shortcode_offer_type_by_brand' );
            }
            add_action( 'init', 'register_shortcodes' );

            /**
             * Offer Shortcode Callback
             * 
             * @param Array $atts
             *
             * @return string
             */

            //Start Offers Shortcode 1

            function shortcode_offer_type_by_brand( $atts ) {

            ob_start();

                global $wp_query,
                    $post;

                $atts = shortcode_atts( array(
                    'posts' => 8, //sets number of posts
                    'pages' => false, //sets pagination
                    'brand' => '', //brand name goes here in shortcode
                    'type' => array( 'offer-type-1','offer-type-2','offer-type-3' ) //offer type is defined, by default show all offer types
                ), $atts );

                if ($atts['pages'] == true ) { //if shortcode defines pages as true, enable pagination

                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

                    $loop = new WP_Query( array(
                        'paged' => $paged,
                        'posts_per_page'    => $atts['posts'],
                        'post_type'         => 'offers',
                        'tax_query'         => array( array(
                            'taxonomy'  => $atts['brand'],
                            'field'     => 'slug',
                            'terms'     => $atts['type']
                        ) )
                    ) );

                } else {
                    $loop = new WP_Query( array( //if shortcode defines pages as false or no value, disable pagination
                        'posts_per_page'    => $atts['posts'],
                        'post_type'         => 'offers',
                        'order'             => 'rand',
                        'tax_query'         => array( array(
                            'taxonomy'  => $atts['brand'],
                            'field'     => 'slug',
                            'terms'     => $atts['type']
                        ) )
                    ) );
                }

                ?>

               <?php

                if( ! $loop->have_posts() ) {
                   ?>
                   <h3>We currently have no offers. Please check back soon.</h3>
                   <?php
                }

                ?>

                <?php

                while( $loop->have_posts() ) {
                    $loop->the_post();

                    ?>
                            <div class="offer">
                                <div class="offerText">
                                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                </div>
                            </div>
                    <?php

                    next_posts_link( '&larr; Older posts', $loop ->max_num_pages);
                    previous_posts_link( 'Newer posts &rarr;' );

                } 

                wp_reset_postdata();

                ?>

                <?php

            return ob_get_clean();

            }

如有任何帮助,我们将不胜感激!谢谢

编辑

使用 Tuhin 的回复,最终使用的代码与他的数组相关,我在 $andor 变量上使用默认值始终是关系 'OR' 除非在简码变量中定义。

            $brands = $atts['brand'];
            $offers = $atts['type'];
            $andor = $atts['andor'];
            $args = [
                'post_type' => 'offers',
                'tax_query' => [
                    'relation' => $andor,
                    [
                        'taxonomy' => 'brand',
                        'field' => 'slug',
                        'terms' => $brands
                    ],
                    [
                        'taxonomy' => 'offer',
                        'field' => 'slug',
                        'terms' => $offers
                    ],
                ]
            ];

我想问你一个问题,但由于我现在没有足够的代表,我将添加一个答案。从您的示例来看,您似乎在每个品牌条款下使用相同的优惠条款(优惠 1、优惠 2、优惠 3),似乎您在每个品牌下重复条款。

我认为您尝试实现它的方式可以通过实际拥有两种不同的分类法来简化。所以对于 post 类型 'offers' 你可以有一个分类 'brand' 和另一个 'offer'.

因此品牌可以具有不同的价值,例如:

  • 品牌 1
  • 品牌 2
  • 品牌 3

然后您可以为 'offers' 分配不同的值,例如:

  • 报价 1
  • 报价 2
  • 报价 3

现在您可以搜索任意组合,例如:

$brands = $atts['brand'];
$offers = $atts['type'];
$args = [
    'post_type' => 'offers',
    'tax_query' => [
        'relation' => 'OR',
        [
            'taxonomy' => 'brand',
            'field' => 'slug',
            'terms' => $brands
        ],
        [
            'taxonomy' => 'offer',
            'field' => 'slug',
            'terms' => $offers
        ],
    ]
];

这将为您 return 提供具有任何这些分类条目的所有商品 post 类型。所以如果你填写了