【问题标题】:WooCommerce: Duplicate shop page to show custom taxonomy instead of categoriesWooCommerce:复制商店页面以显示自定义分类而不是类别
【发布时间】:2018-04-23 19:05:28
【问题描述】:

在 WooCommerce 中,我使用以下代码为我的产品创建了一个完美运行的自定义分类:

// Register Custom Taxonomy
function ess_custom_taxonomy_Item()  {

$labels = array(
    'name'                       => 'Collections',
    'singular_name'              => 'Collection',
    'menu_name'                  => 'Collections',
    'all_items'                  => 'All Collections',
    'parent_item'                => 'Parent Collection',
    'parent_item_colon'          => 'Parent Collection:',
    'new_item_name'              => 'New Collection Name',
    'add_new_item'               => 'Add New Collection',
    'edit_item'                  => 'Edit Collection',
    'update_item'                => 'Update Collection',
    'separate_items_with_commas' => 'Separate Collection with commas',
    'search_items'               => 'Search Collections',
    'add_or_remove_items'        => 'Add or remove Collections',
    'choose_from_most_used'      => 'Choose from the most used Collections',
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'collection', 'product', $args );

}

add_action( 'init', 'ess_custom_taxonomy_item', 0 );

我现在尝试创建一个完全模拟标准 woocommerce“商店”页面(显示每个顶级类别)但显示此分类而不是类别的页面。

如何最好地做到这一点?我想在 WooCommerce 中复制商店页面模板并替换一些(?)调用,或者创建 Woo 短代码之一的副本(可能来自插件?)。

顺便说一句,我使用 ACF 为每个新的“集合”分类法分配了一个图像字段,我想我需要将其拉入前端以实现上述目标。

【问题讨论】:

    标签: wordpress woocommerce custom-taxonomy


    【解决方案1】:

    最后我设法通过复制和重命名主题页面模板来做到这一点,包括产品网格的标记并获取分类术语和 ACF 图像,如下所示.. 这里的 foreach 代码非常有用 - 希望这是对下线的人有用。

    <?php
    
    // Get Terms for Custom Taxonomy
    $taxonomy = 'collection';
    $tax_terms = get_terms($taxonomy);
    
    ?>
    
    <!-- Duplicated Product Grid Markup -->
    <div class="col-sm-12">
        <div class="row">
            <ul class="products columns-4">
                <div class="col-sm-4">
    
    <?php
    // Create grid item for each instance of the Custom Taxonomy
    foreach ($tax_terms as $tax_term) {
    
        echo '<li class="product-category product">';
        echo '<a href="/collection/' . $tax_term->slug . '">';
    
        // Grab & insert the ACF thumbnail 'collection_image'
        $thumbnail = get_field('collection_image', $tax_term->taxonomy . '_' . $tax_term->term_id);
        echo '<img src="' . $thumbnail . '" alt="' . $tax_term->name . '" width="300" height="300" />';
    
        // Tie it all up
        echo '<h3>' . $tax_term->name . '</h3>';
        echo '</a></li>';
    
    }
    ?>
    
                </div>
            </ul>
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 2019-09-13
      相关资源
      最近更新 更多