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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化