2016-03-06 43 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

相关问题