【问题标题】:Usable woocommerce template for custom taxonomy用于自定义分类的可用 woocommerce 模板
【发布时间】:2018-07-17 06:13:54
【问题描述】:

我为我的 Woocommerce 商店创建了一个自定义分类法book-author,现在我正在尝试结合一个存档模板,让它像普通的 Woo 存档一样显示前端。昨天我发现了这段代码,它帮助我在点击时将分类法从 404 错误中解脱出来,但它返回了一个带有 No product 通知的商店页面(尽管我点击了一个现有的分类法) .

重点是,book-author分类法是小标签的母体,或者“作者”,所以我需要修复这个标签或者想办法让它对母体通用book-author 及其所有的孩子/作者。

add_filter('template_include', 'team_set_template');
function team_set_template( $template ){
  if(is_tax('book-author')) :
    $taxonomy = 'book-author';
    $term = get_query_var($taxonomy);
    $prod_term = get_terms($taxonomy, 'slug='.$term.''); 
    $term_slug = $prod_term[0]->slug;
    $t_id = $prod_term[0]->term_id;
    $term_meta = get_option( "taxonomy_$t_id" );
    $term_meta['bookauthor_access_pin'];  

    wc_get_template( 'archive-product.php' );

 else : 

    wc_get_template( 'archive-product.php' );

 endif; 

}

我尝试复制archive-product.php,将其重命名为taxonomy-book-author.php 并将其放入我的子主题文件夹中。这似乎是一种更好的方法,但没有结果 - 仍然是 404。

book-author 之所以是标签,而不是类别,是因为作者没有层次结构。而且我知道有这个(工具集)的插件,但他们在那里将免费版本升级为付费版本,所以我试图找到一种更手动和永久的方式。

提前谢谢你们,伙计们。

【问题讨论】:

    标签: php wordpress templates woocommerce custom-taxonomy


    【解决方案1】:

    仅在审查和尝试您的代码时就会出现许多错误......

    注意:过滤器挂钩函数总是需要返回一些东西。

    在您的代码中: - get_query_var($taxonomy) 将始终返回一个术语“slug” - $term_meta['bookauthor_access_pin']; 没用,我真的不知道有什么用。

    您说book-author 是标签而不是类别的原因”...如果您创建了自定义分类book-author,这不能是产品类别或产品标签(woocommerce 自定义分类法)……

    所以您应该尝试以下代码(没有任何保证,因为您没有提供所有相关代码,并且无法测试):

    add_filter('template_include', 'team_set_template');
    function team_set_template( $template ){
    
        $taxonomy = 'book-author';
    
        if( ! is_tax( $taxonomy ) ) return $template;
    
        $term_slug = get_query_var($taxonomy);
    
        if ( empty( $term_slug ) ) return $template;
    
        $term = get_term_by('slug', $term_slug, $taxonomy );
    
        if ( is_wp_error( $term ) ) return $template;
    
        $term_id = $prod_term->term_id;
        $term_meta = get_option( 'taxonomy_'. $term_id );
    
        // $term_meta['bookauthor_access_pin']; //  ???
    
        return wc_locate_template( 'archive-product.php' );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      相关资源
      最近更新 更多