2016-03-06 23 views
0

我想在用戶輸入時有條件地使用文件。我在我的孩子主題中使用WooCommerce模板。當然,該文件(content-single-product.php)是在兒童主題中自定義的。不過,我也希望給用戶一個選擇,如果他們想使用默認的WC文件。總之,我想有條件地使用那個用戶將選擇的文件。如何在WordPress中有條件地使用文件?

我正在使用PHP copy()unlink()函數。我只想要你的專家意見,如果這是最好的解決方案,或者如果你有比我更好的建議。這裏是我的孩子主題中的功能。

/*...for copying the file from a child theme folder to the woocommerce folder...*/ 
function ac_wc_files_to_theme() 
{ 
    $theme_dir = get_stylesheet_directory() . '/woocommerce/files/content-single-product.php'; 
    $theme_dir_file = get_stylesheet_directory() . '/woocommerce/content-single-product.php'; 

    if (!copy($theme_dir, $theme_dir_file)) { 
     echo "failed to copy $theme_dir to $theme_dir_file...\n"; 
    } 
} 

/*...for removing the file from a the woocommerce folder...*/ 

function ac_wc_delete_wc_file(){ 

    $fileArray = array(
    get_stylesheet_directory() . '/woocommerce/content-single-product.php' 
    ); 

foreach ($fileArray as $value) { 
    if (file_exists($value)) { 
     unlink($value); 
    } else { 
     echo 'file not found'; 
    } 
} 
} 

/*...calling the options from the theme settings area....*/ 


if (get_option('ac_wc_default_single') == 1) { 
add_action('init', 'ac_wc_delete_wc_file'); 
remove_action('init', 'ac_wc_files_to_theme'); 
} 
else { 
add_action('init', 'ac_wc_files_to_theme'); 
remove_action('init', 'ac_wc_delete_wc_file'); 
} 

該代碼工作正常。只是我需要你的看法,如果這樣做。謝謝

+0

而不是複製的文件的時候,我想你也許能夠通過過濾['woocommerce_locate_template']實現自己的主題和模板woocommerce版本之間切換的目標(https://github.com/woothemes/ woocommerce /斑點/ 927941e2cad28903a2d0bd03ce52a8809525f4e3 /包括/ WC-芯的functions.php#L256)。 – helgatheviking

+0

謝謝。我嘗試過,但這似乎並不奏效。你能建議嗎? 功能ac_wc_override_single_product($模板,$ TEMPLATE_NAME,$ template_path){ 如果($ TEMPLATE_NAME == '內容單product.php'){$ = template_path get_stylesheet_directory()。 '/woocommerce/files/content-single-product-1.php'; $ template = $ template_path; } return $ template; } add_filter('woocommerce_locate_template','ac_wc_override_single_product',20,3); – Ayanize

+0

將來,你可以編輯你的問題來添加新的代碼嗎?評論中的代碼非常難以閱讀。 – helgatheviking

回答

0

這是我最好的猜測過濾模板沒有廣泛的測試。

// change the default template path to `woocommerce/files` 
function ac_wc_override_template_path(){ 
    return 'woocommerce/files/'; 
} 
add_filter('woocommerce_template_path', 'ac_wc_override_template_path'); 


// now filter the woocommerce template 
function ac_wc_override_single_product($template, $template_name, $template_path) { 
    $alt_template = ''; 

    if ($template_name == 'content-single-product.php') { 
     $alt_template = locate_template(trailingslashit($template_path) . 'content-single-product-1.php'); 
    } 

    if($alt_template && get_option('use_my_themes_templates')){ 
     return $alt_template; 
    } else { 
     return $template; 
    } 
} 
add_filter('woocommerce_locate_template', 'ac_wc_override_single_product', 20, 3); 
+0

感謝它sorta爲我工作,但我得到通知未定義的變量:alt_template當選項未被選中,意味着當用戶想要使用WC的默認模板。沒有問題,因爲我現在有條件地添加過濾器 – Ayanize

+0

啊......這是因爲如果你不是'content-single-product.php',那麼'$ alt_template'沒有被定義。這很容易解決,看看我的編輯。 – helgatheviking

相關問題