wc_get_template返回null

时间:2016-08-08 12:51:25

标签: php wordpress woocommerce

我似乎无法让wc_get_template()返回我的网页模板。我确信路径是正确的,我将它存储在我的子主题文件夹中,但当我运行它时,我得到NULL。你能在这里看到什么问题吗?

public function endpoint_content() { 

    // The template name. 
    $template_name = 'Students Details'; 

    // (default: array()) 
    $args = array(); 

    // The template path. 
    $template_path =  get_stylesheet_directory().'/woocommerce/myaccount/add_students.php';

    // NOTICE! Understand what this does before running. 
    $res = wc_get_template($template_name, $args, $template_path, $template_path);

    var_dump($res); 
}

1 个答案:

答案 0 :(得分:3)

您将错误的参数传递给wc_get_template()

  1. $template_name是全名myaccount/student-details.php
  2. $template_path是一个空字符串。通常,WC会查看主题的woocommerce文件夹
  3. $default_path这是插件模板的路径。在这种情况下,将templates文件夹添加到我在original question
  4. 中为您创建的插件的根文件夹中

    这是更新后的功能:

    /**
     * Endpoint HTML content.
     */
    public function endpoint_content() {    
    
        // The template name. 
        $template_name = 'myaccount/student-details.php'; 
    
        // default args
        $args = array(); 
    
        // default template
        $template_path = ''; // use default which is usually "woocommerce"
    
        // default path (look in plugin file!)
        $default_path = untrailingslashit( plugin_dir_path(__FILE__) ) . '/templates/';
    
        // call the template
        wc_get_template($template_name, $args, $template_path, $default_path);
    
    }
    

    这是一个示例模板wc-your-custom-endpoint-plugin/templates/student-details.php

    <?php
    /**
     * Template
     * 
     * @author      Kathy Darling
     * @package     WC_Custom_Endpoint/Templates
     * @version     0.1.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    ?>
    
    <?php do_action( 'wc_custom_endpoint_before_student_details' ); ?>
    
    <p><?php _e( 'Hello World!', 'wc-custom-endpoint' ); ?><p>
    
    <?php do_action( 'wc_custom_endpoint_after_student_details' ); ?>