如何在 wordpress 页面中显示“类别产品下拉菜单"?在 Woocommerce 2.5.2 中 [英] How do I display a "category products drop down in a wordpress page" in Woocommerce 2.5.2

查看:37
本文介绍了如何在 wordpress 页面中显示“类别产品下拉菜单"?在 Woocommerce 2.5.2 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个类别中产品的下拉菜单.

I would like to display a drop down menu for products in a category.

<select>
  <option value="CODE HERE">Volvo</option>
</select> 

所以根据 Wordpress 编码..

So according to Wordpress coding..

<?php

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

好的,所以我进一步调查,我希望根据 https://developer.wordpress.org 我正在使用名为 NOVA WP 的 Storefront 子主题.

Okay so I investigated further and I am looking to do a single page template according to https://developer.wordpress.org I am using a child theme for Storefront which is called NOVA WP.

为了制作这个单页模板",我复制了 page.php 并将其重命名为 page-buildit.php

To make this "single page template" I copied page.php and renamed it to page-buildit.php

这是我实际编辑代码的Mypage.我确实复制了代码,但结果是空白

This is Mypage in which I actually editing the code. I did copy the code but it turns out blank

发现这个:WooCommerce:创建一个短代码显示产品类别但我不明白的是我们不能再用新的 wordpress 版本这样做了.

found this: WooCommerce: Create a shortcode to display product categories but my undestanding is we cant do this anymore with the new wordpress version.

推荐答案

<?php
$args = array(
    'order'      => 'ASC',
    'hide_empty' => $hide_empty,
    'include'    => $ids,
    'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo "<select>";
foreach( $product_categories as $category ){
    echo "<option value = '" . esc_attr( $category->slug ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
?>

看看这个.这是获取产品类别的方法.

Check this out. This is the way to get product categories.!

这篇关于如何在 wordpress 页面中显示“类别产品下拉菜单"?在 Woocommerce 2.5.2 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆