【问题标题】:Get specific Product for each category of a brand in WordPress Custom Post Types在 WordPress 自定义帖子类型中为品牌的每个类别获取特定产品
【发布时间】:2017-07-07 10:37:22
【问题描述】:

我有一个名为 products 的自定义帖子类型,它的分类名为 category

register_post_type( 'products',
        array(
            'labels' => array(
                'name' => __( 'Products' ),
                'singular_name' => __( 'Product' ),
                'add_new_item' => __( 'Add New Product' ),
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'Products'),
            'taxonomies'  => array( 'category' )
        )
    );

我还有一个是brands

register_post_type( 'brands',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Brands' ),
                'singular_name' => __( 'BRAND' ), 
                'add_new_item' => __( 'Add New Brand' ),
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'BRANDS'),
            'taxonomies'  => array( 'category' ),
        )
    );

我有一个名为 page-brands.php 的页面,我使用以下代码获取所有品牌。

$args = array( 
    'post_type' => 'brands', 
    'posts_per_page' => -1, 
    'order' => 'ASC'
);
$brands = new WP_Query( $args );

然后我显示品牌名称和徽标..

<a class="hvr-grow" href="<?php the_permalink(); ?>">
   <img src="<?php the_field('brand_logo'); ?>">
   <div class="brands_title"><?php the_title(); ?></div>
</a>

点击链接的品牌名称后,效果很好。它转到single-brands.php 页面。例如,我们点击了品牌名称“LG”。它将进入单一品牌页面,并显示其第一类的所有产品。

我得到了它的第一个类别,并通过另一个函数将其保存在 first_brand_category 中,然后在下面的查询中使用来显示。

$proucts_args = array(
    'posts_per_page' => 50,
    'post_type' => 'products',
    'cat' => $first_brand_category,
);
$products_query = new WP_Query($proucts_args);

一切正常,直到完美。但现在我想更改上面的查询,因为我想为每个类别显示一个特定的产品。比如……

品牌是LG LG 品牌的类别为 TVMobilesAir Conditioners 等 电视类别有多种产品,手机等有多种产品。

我想通过该类别的超链接显示一种针对 LG 的特定电视产品、针对 LG 的一种特定手机产品和针对 LG 的一种特定空调产品。

我该怎么做?任何建议,我是 wordpress 新手

【问题讨论】:

    标签: php mysql magento custom-post-type


    【解决方案1】:

    这取决于您的需要。

    如果您知道需要展示什么特定产品: 一种方法是在参数中传递产品 id,Wp_query 将只返回该产品。请参考:https://developer.wordpress.org/reference/classes/wp_query/。或者,您也可以对产品进行排序并通过将 1 赋予 post_count 参数来仅选择一种产品。

    【讨论】:

    • 我已经读过这篇文章,尝试了不同的解决方案,但对我没有任何效果,也不确定哪个是正确的。
    • 会有很多产品。您应该知道需要展示哪些产品。
    【解决方案2】:

    这绝对可以帮助您获得特定类别的帖子。

    $args = array(
                            'post_type' => 'post type name',
                            'post_status' => 'publish',
                            'posts_per_page' => -1,
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'Post Taxonomy name',
                                    'field'    => 'slug',
                                    'terms'    => 'your slug' 
    
                              ),
                            ),
                        );
                        $loop = new WP_Query( $args );
    

    如果您想按名称调用您的分类法,请使用它 '字段' => '名称', 'terms' => '你的猫名'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 2012-05-17
      • 2018-08-19
      • 1970-01-01
      相关资源
      最近更新 更多