【问题标题】:How do I override a template file in a Plugin?如何覆盖插件中的模板文件?
【发布时间】:2019-10-17 15:45:06
【问题描述】:

我想覆盖 WooCommerce 产品滑块插件中的模板文件。 PHP 文件的路径是 plugins/woocommerce-products-slider/templates/wcps-title.php

我该怎么做?

我尝试在我的子主题中使用相同的路径结构,但它不起作用。

这是原始文件中的代码:

<?php 

/** 
* @Author         ParaTheme 
* Copyright:     2015 ParaTheme 
*/ 

if ( ! defined('ABSPATH')) exit;  // if direct access  

  $title_text = apply_filters( 'wcps_filter_title', get_the_title(get_the_ID()) ); 

  $html.= '<div class="wcps-items-title" >
      <a style="color:'.$wcps_items_title_color.';font-size:'.$wcps_items_title_font_size.'" href="'.$permalink.'">
        '.$title_text.'
      </a>
  </div>'; 

我想在代码中添加&lt;span class="set-field"&gt;'. get_field('set') .'&lt;/span&gt;,使其看起来像:

<?php 

/** 
* @Author         ParaTheme 
* Copyright:     2015 ParaTheme 
*/ 

if ( ! defined('ABSPATH')) exit;  // if direct access  

  $title_text = apply_filters( 'wcps_filter_title', get_the_title(get_the_ID()) ); 

  $html.= '<div class="wcps-items-title" >
      <a style="color:'.$wcps_items_title_color.';font-size:'.$wcps_items_title_font_size.'" href="'.$permalink.'">
        <span class="set-field">
            '. get_field('set') .'
        </span>
        '.$title_text.'
      </a>
  </div>';

【问题讨论】:

    标签: php wordpress plugins


    【解决方案1】:

    我经常在我的插件文件中使用一个函数来处理特定的模板以防止它被覆盖。

    add_filter( 'template_include', 'assign_template');
    
    function assign_template($template){
        if($template == 'old_template.php'){
            $template = get_stylesheet_directory() . '/woocommerce-products-slider/wcps-title-custom.php';
       }
       return $template;
    }
    

    【讨论】:

    • 您好 KGreene,感谢您的回复 :) 当我使用此功能时,网站无法正常工作。它只显示一条消息“该站点遇到技术问题。”
    • 哎呀。删除“公众”
    • 嗯,它似乎仍然没有覆盖旧模板。
    • 你可以尝试改变优先级 add_filter( 'template_include', 'assign_template', 100, 1);
    • 还是不行。可能是因为我的新模板文件的路径结构?它看起来像这样 /child-theme/woocommerce-products-slider/wcps-title-custom.php 到目前为止我的代码在我的函数 php 中看起来像这样: add_filter( 'template_include', 'assign_template', 100, 1) ; function assign_template($template){ if($template == 'wcps-title.php'){ $template = trailingslashit( plugin_dir_path( FILE ) ) 。 'wcps-title-custom.php'; } 返回$模板; }
    猜你喜欢
    • 1970-01-01
    • 2021-08-12
    • 2013-02-26
    • 2018-06-03
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    相关资源
    最近更新 更多