首页 文章

如何在Woocommerce 2.5.2中显示“在wordpress页面中下载类别产品”

提问于
浏览
0

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

<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做一个单页模板我正在使用Storefront的一个名为NOVA WP的子主题 .

为了使这个"single page template"我复制了page.php并将其重命名为 page-buildit.php

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

发现这个:WooCommerce: Create a shortcode to display product categories但我的未知是我们不能用新的wordpress版本做到这一点 .

3 回答

  • 1
    <?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>";
    ?>
    

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

  • 2

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

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

    或者,如果要将输出保存在变量中,可以使用参数'echo'=> 0然后回显变量以获得相同的输出 .

    $args = array('hide_empty'=> 0,
        'taxonomy'=> 'product_cat',
        'hierarchical'=>1,
        'echo'=>0);
    
    $cats = wp_dropdown_categories($args);
    echo $cats;
    
  • 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>";
    ?>
    

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

相关问题