我想在用户输入时有条件地使用文件。我在我的孩子主题中使用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');
}
该代码工作正常。只是我需要你的看法,如果这样做。谢谢
而不是复制的文件的时候,我想你也许能够通过过滤['woocommerce_locate_template']实现自己的主题和模板woocommerce版本之间切换的目标(https://github.com/woothemes/ woocommerce /斑点/ 927941e2cad28903a2d0bd03ce52a8809525f4e3 /包括/ WC-芯的functions.php#L256)。 – helgatheviking
谢谢。我尝试过,但这似乎并不奏效。你能建议吗? 功能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
将来,你可以编辑你的问题来添加新的代码吗?评论中的代码非常难以阅读。 – helgatheviking