我目前正在开发一个 WordPress 网站,选择的购物平台是 WooCommerce。
我想创建一系列模板文件,在content-product-single.php
文件,其中包含数学计算。由于某种原因,数学计算似乎只有在放入functions.php
而不是我指定的模板文件。有人建议我使用的方式get_template_part
标签可能不正确。这是我使用的原始代码:
<?php
if (is_product_category('category-name')) {
get_template_part( 'templates/template-a' );
}
elseif (is_single('product-slug')) {
get_template_part( 'templates/template-b' );
}
elseif (is_single('product-slug')) {
get_template_part( 'templates/template-c' );
}
?>
如果有人注意到我的上述代码中有任何错误,请告诉我,因为这可能是问题所在,但目前,我认为我的数学编码可能只能在functions
file.
出于组织目的,有没有办法可以创建多个functions.php
文件还是这是不好的做法?我努力了functions-template-file-name.php
并将其放在主题的根中,但数学仍然不起作用。
You can include http://php.net/manual/es/function.include-once.php像这样的辅助文件在你的functions.php
(子目录中的文件inc
):
include_once __DIR__ . '/inc/shortcodes.php';
include_once __DIR__ . '/inc/frontend.php';
On functions.php
(和包含的文件)您应该只添加挂钩(过滤器和操作)及其各自的函数回调。
在模板文件(及其get_template_part
s) 你留下 HTML 并最终调用你的主题的函数,比如
<div class="results">
<?php my_get_results(); ?>
</div>
Where my_get_results()
(在functions.php中)会是这样的:
function my_get_results(){
$results = get_something();
foreach($results as $r) {
echo '<span>' . $r . '</span>';
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)