wc_get_template 返回 null

Ale*_*wan 3 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); 
}
Run Code Online (Sandbox Code Playgroud)

hel*_*ing 6

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

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

这是更新的功能:

/**
 * 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);

}
Run Code Online (Sandbox Code Playgroud)

这是一个示例模板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' ); ?>
Run Code Online (Sandbox Code Playgroud)