drj26159 2017-05-18 23: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 23: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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭