dsfsfdsf4544 2016-08-08 12:51
浏览 30
已采纳

wc_get_template返回null

I can't seem to get the wc_get_template() to return my page template. I am sure the path is correct to where I have it stored in my child theme folder but when I run it I get NULL. Can you see anything wrong here?

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条回答 默认 最新

  • douluo6626 2016-08-08 17:18
    关注

    You are passing the wrong arguments to wc_get_template().

    1. $template_name is the full name so myaccount/student-details.php
    2. $template_path is an empty string. Usually, WC will look in the theme's woocommerce folder
    3. $default_path this is the path to your plugin's templates. In this case add a templates folder into the root folder of the plugin I created for you in your original question

    Here's the updated function:

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

    And here's a sample template 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' ); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路