Javascript 如果商品类别中的商品计数为零,则重定向自定义页面
如果Woocommerce类别中的产品计数为零,我需要重定向自定义页面。请帮助我您只需添加$cat->Count即可获得该类别中所有产品的计数。希望这对你有所帮助Javascript 如果商品类别中的商品计数为零,则重定向自定义页面,javascript,wordpress,woocommerce,hook-woocommerce,Javascript,Wordpress,Woocommerce,Hook Woocommerce,如果Woocommerce类别中的产品计数为零,我需要重定向自定义页面。请帮助我您只需添加$cat->Count即可获得该类别中所有产品的计数。希望这对你有所帮助 $args = array( 'number' => $number, 'orderby' => $orderby, 'order' => $order, 'hide_empty' => $hide_empty, 'include' =>
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms( 'product_cat', $args );
foreach( $product_categories as $cat ) {
echo $cat->name.' ('.$cat->count.')';
}
谢谢假设您通过分类术语页面之一访问产品类别: 将其添加到
functions.php
文件中。确保替换某些路径/一些自定义文件.php'代码>包含要加载的模板的路径
add_filter( 'template_include', 'redirect_on_empty_product_category' );
function redirect_on_empty_product_category( $template ){
if ( ( $taxonomy_id = get_query_var( 'taxonomy' ) ) && ( $term_id = get_query_var( 'term' ) ) && is_tax( $taxonomy_id, $term_id ) ){
$term = get_term_by( 'slug', $term_id, $taxonomy_id );
if ( $term->count === 0 ){
$page_url = get_permalink($some_post_id);
echo sprintf('<script type="text/javascript"> window.location = "%s" </script>', $page_url);
}
}
return $template;
}
add_filter('template_include','redirect_on_empty_product_category');
函数重定向\u关于\u空\u产品\u类别($template){
if($taxonomy\u id=get\u query\u var('taxonomy')&($term\u id=get\u query\u var('term'))&&is\u tax($taxonomy\u id,$term\u id)){
$term=get_term_by('slug',$term_id,$taxonomy_id);
如果($term->count==0){
$page\u url=get\u permalink($some\u post\u id);
echo sprintf('window.location=“%s”,$page\u url);
}
}
返回$template;
}
Hi我可以使用页面id或slugSure重定向到自定义页面而不是某些模板吗?请参见我的编辑到您的编辑,我添加了$page\u url=get\u permalink($some\u post\u id)
并将echo语句转换为sprintf以提高可读性。只需确保$some\u post\u id
是真实的页面id