我有一个产品类别'免费优惠'。我想限制用户直接访问此类别及其产品。我的意思是,如果用户尝试通过网址http://www.mysite.com/product-category/free-offers或http://www.mysite.com/product-category/free-offers/free-prodcut访问它,则会收到404错误。有可能吗?
答案 0 :(得分:2)
我做了一些事情并且工作正常,也许对其他人有用。这是
function disable_free_offers_access() {
global $wp_query, $post;
$tax = 'product_cat';
$term = 'coffee-tea';
$post_type = 'product';
$term_obj = get_term_by('slug', $term, $tax);
$termchildren = get_term_children($term_obj->term_id, $tax);
if (is_tax()) {
if (is_tax($tax, $term) || term_is_ancestor_of($term_obj->term_id, $wp_query->get_queried_object()->term_id, $tax)) {
load_template(TEMPLATEPATH . '/404.php');
exit;
}
} else if (is_singular($post_type)) {
$terms = wp_get_post_terms($post->ID, $tax);
if (!empty($terms))
foreach ($terms as $t) {
if ($t->term_id == $term_obj->term_id || in_array($t->term_id, $termchildren)) {
load_template(TEMPLATEPATH . '/404.php');
exit;
}
}
}
}
add_action('template_redirect', 'disable_free_offers_access');