我为我的Woocommerce商店创建了一个自定义分类书作者,现在我正在尝试联合一个存档模板,以便它像普通的Woo存档一样显示前端。昨天我找到了这个代码,它帮助我在单击时将分类法从 404 错误中解脱出来,但它返回了一个没有产品通知的商店页面(尽管我点击了现有的分类法之一)。
关键是,书籍作者分类法是小标签或"作者"的母亲,所以我需要修复这个标签或找到一种方法来使其对母书作者及其所有孩子/作者具有普遍性。
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
之所以是一个标签,而不是一个类别,是因为作者没有层次结构。我知道有这个插件(工具集),但他们将免费版本升级到付费版本,所以我试图找到一种更手动和永久的方式。
提前谢谢你们,伙计们。
在查看和尝试代码时有很多错误......
注意:过滤器挂钩函数需要始终返回一些内容。
在您的代码中: -get_query_var($taxonomy)
总是会返回一个术语"蛞蝓">-$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' );
}