2016-02-18 66 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; 
相關問題