2016-02-18 63 views
0

我想显示类别中产品的下拉菜单。如何在Woocommerce 2.5.2中显示“在wordpress页面中下载类别产品”

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

所以根据WordPress的编码..

<?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。

为了使这个“单页模板”我复制page.php文件,并将其重命名为page-buildit.php

这是Mypage中,我实际上是在编辑代码。我没有复制代码,但事实证明空白

找到这个:WooCommerce: Create a shortcode to display product categories 但我的未知是我们不能再这样做新的wordpress版本。

回答

2
<?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>"; 
?> 

看看这个。这是获得产品类别的方法。

+0

我在哪里输入此内容? – ingalcala

+0

仪表板>产品>分类 –

+0

我不明白。我的主要目标是:在Wordpress页面中显示不在Wordpress模板中的类别列表。 – ingalcala

0

好的,这里是我解决它的方法,在Hemnath mouli的帮助下,我已经给了你答案的功劳,但我想在Dropbox里发布一个类别里的产品。

$args = array(
'posts_per_page' => -1, 
'product_cat' => 'motherboard', 
'post_type' => 'product', 
'orderby' => 'title', 
); 
$products = new WP_Query($args); 
echo "<select>"; 
foreach ($products as $product) { 
$products->the_post(); 
?>  
<option value="<?php the_permalink(); ?>"> <?php the_title(); ?>  
<?php 
} 
echo "</select>"; 
?> 

现在我需要在选择它之后显示该产品的图像。

1

您还可以使用函数wp_dropdown_categories使您的代码更简单。 要获得您可以这样写的产品类别的下拉菜单。

$args = array('hide_empty'=> 0, 
    'taxonomy'=> 'product_cat', 
    'hierarchical'=>1); 

wp_dropdown_categories($args); 

或者,如果你想保持在可变输出,你可以使用参数“回声” => 0,然后回显变量获得相同的输出。

$args = array('hide_empty'=> 0, 
    'taxonomy'=> 'product_cat', 
    'hierarchical'=>1, 
    'echo'=>0); 

$cats = wp_dropdown_categories($args); 
echo $cats; 
相关问题