如何覆盖插件中的模板文件?

时间:2019-10-17 15:45:06

标签: php wordpress plugins

我想覆盖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>'; 

我想将<span class="set-field">'. get_field('set') .'</span>添加到代码中,使其看起来像:

<?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>';

1 个答案:

答案 0 :(得分:0)

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

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;
}
相关问题