Предполагается, что вы получаете доступ к категориям продуктов через одну из страниц терминов таксономии:
Добавьте это в файл functions.php
. Обязательно замените SOME_PATH . '/some-custom-file.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;
}