redirect category with single product to product page?

Modern e-commerce WordPress theme to build successful online store with WooCommerce support.
GK User
Mon Nov 04, 2013 5:33 am
Hi I am trying to redirect a category with single product to a single product page?
I have tried the code below with no success. The code below i added to wc-functions.php.
Can anyone please give advise? Regards Michael

Code: Select all
//   redirect
   add_action('category_template', 'use_single_for_one_category');
function use_single_for_one_category($template = '') {
        global $wp_query;
        if ( 1 === (int) $wp_query->post_count ) {
                $template = get_single_template();
        }
        return $template;
}


I have also tried some of the code here : http://wordpress.org/support/topic/wooc ... oduct-page
User avatar
Junior Boarder

GK User
Mon Nov 04, 2013 6:42 am
I found a solution:

I included the code below to functions.php (/wp-content/themes/ShopAndBuy-Child/gavern)

Code: Select all
add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
   
        global $wp_query;
        if ($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
            exit;
        }
    }
User avatar
Junior Boarder


cron