在产品类别上获取wp_Query的类别名称/子类别,以根据类别显示产品列表

时间:2019-01-23 11:26:21

标签: php wordpress wordpress-theming

我正在开发woocommerce网站,我想根据产品类别页面上的类别显示产品列表,即class AlbumCreateSheet: BottomSheetDialogFragment(), View.OnClickListener { companion object { private val tag = AlbumCreateSheet::class.java.simpleName fun show(fragmentManager: FragmentManager) { return AlbumCreateSheet().show(fragmentManager, tag) } } private lateinit var titleView: EditText private lateinit var descriptionView: EditText override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) return inflater.inflate(R.layout.sheet_album_create, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) titleView = view.findViewById(R.id.album_create_title) descriptionView = view.findViewById(R.id.album_create_description) view.findViewById<View>(R.id.album_create_create).setOnClickListener(this) } override fun onClick(v: View?) { when(v?.id) { R.id.album_create_create -> { useDefaultRealm { executeTransaction { val session = Session(name = titleView.text.toString(), description = descriptionView.text.toString()) it.insertOrUpdate(session) } } dismiss() } } } } 。我想在此链接上显示所有女士产品,但是如何显示?我的代码无效。

我正在使用此功能www.wesiteName.com/product-category/ladies来获取类别名称,但它会打印类别名称,但是在wp查询中没有传递名称,这不知道为什么。

woocommerce_page_title()

它显示了所有类别的所有产品。我只想根据类别显示特定的产品列表。

1 个答案:

答案 0 :(得分:0)

尝试此代码。

$args = array(
'number'     => $number,
'orderby'    => 'title',
'order'      => 'ASC',
'hide_empty' => $hide_empty,
'include'    => $ids
 );
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
    echo '<h4><a href="' . get_term_link( $product_category ) . '">' . 
$product_category->name . '</a></h4>';
    $args = array(
        'posts_per_page' => -1,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                // 'terms' => 'white-wines'
                'terms' => $product_category->slug
            )
        ),
        'post_type' => 'product',
        'orderby' => 'title,'
    );
    $products = new WP_Query( $args );
    echo "<ul>";
    while ( $products->have_posts() ) {
        $products->the_post();
        ?>
            <li>
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>
            </li>
        <?php
    }
    echo "</ul>";
    }
    }

希望它对您有用