0

我有一个产品类别“免费优惠”。我想限制用户直接访问此类别及其产品。我的意思是,如果用户尝试通过 url http://www.mysite.com/product-category/free-offershttp://www.mysite.com/product-category/free-offers/free-prodcut访问它,他们将收到 404 错误。可能吗?

4

1 回答 1

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');
于 2013-10-23T13:46:42.223 回答