drj26159 2017-05-18 15:11
浏览 49
已采纳

有可能分解一个functions.php文件吗?

I am currently working on a WordPress website, where the chosen shopping platform is WooCommerce.

I want to create a series of Template files, to be called within the content-product-single.php file, which contains Maths calculations. For some reason, the Maths calculations only seems to work when placed in the functions.php rather than my assigned Template files. It was suggested that the way in which I was using the get_template_part tags could be incorrect. Here is the original code I was using:

<?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' );   
    }
?>

If anyone notices any errors in my above code, then please do let me know as that could be the issue but for the time being, I think that my Maths coding may only work within a functions file.

For organisation purposes, is there a way I can create multiple functions.php files or would this be bad practice? I have tried functions-template-file-name.php and placed this in the root of the theme but still the Maths does not work.

  • 写回答

2条回答 默认 最新

  • doumao8355 2017-05-18 15:25
    关注

    You can include secondary files like this in your functions.php (files inside the subdirectory inc):

    include_once dirname(__FILE__) . '/inc/shortcodes.php';
    include_once dirname(__FILE__) . '/inc/frontend.php';
    

    On functions.php (and included files) you should only add hooks (filters and actions) and their respective function callbacks.

    On the template files (and their get_template_parts) you leave the HTML and eventually some call to a function of your theme, like

    <div class="results">
    <?php my_get_results(); ?>
    </div>
    

    Where my_get_results() (in functions.php) would be something like:

    function my_get_results(){
       $results = get_something();
       foreach($results as $r) {
            echo '<span>' . $r . '</span>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部