【问题标题】:Flatsome for Woocommerce: Annul file within INC folderWoocommerce 的 Flatsome:INC 文件夹中的 Annul 文件
【发布时间】:2020-05-10 05:44:04
【问题描述】:

我正在尝试修改里面的一部分代码:themes / flatsome / inc / shortcodes / ux_products.php

我想改变这个:

<a href="<?php echo get_the_permalink(); ?>">

为此

<a href="<?php echo get_the_permalink(); ?>" aria-label="<?php echo get_the_title(); ?>">

我的子主题文件夹中有相同的路径和文件,但我无法取消主主题文件。

我意识到有另一个文件引用了我要取消的文件:themes / flatsome / inc / init.php:

if (is_woocommerce_activated()) {
  require get_template_directory() . '/inc/shortcodes/ux_products.php'; //File to annul
  require get_template_directory() . '/inc/shortcodes/ux_products_list.php';
  require get_template_directory() . '/inc/shortcodes/product_flip.php';
  require get_template_directory() . '/inc/shortcodes/product_categories.php';
  if(get_theme_mod('product_layout') == 'custom') {
    require get_template_directory() . '/inc/shortcodes/custom-product.php';
  }
}

如何在不修改主题的情况下做到这一点(这可行但不正确)。

【问题讨论】:

    标签: php wordpress woocommerce wordpress-theming


    【解决方案1】:

    首先,您需要将需要修改的简码复制到您的子主题中。

    'your_child_theme/inc/shortcodes/ux_products.php'
    

    那你需要修改子简码,函数名:

    function ux_products($atts, $content = null, $tag)
    

    function child_ux_products($atts, $content = null, $tag)
    

    修改你需要的:

    <a href="<?php echo get_the_permalink(); ?>">
    

    <a href="<?php echo get_the_permalink(); ?>" aria-label="<?php echo get_the_title(); ?>">
    

    并在您的子简码文件的末尾,使用您自己的函数覆盖所有(或仅需要)声明的简码:

    add_shortcode("ux_products", "ux_products");
    

    add_shortcode("ux_products", "child_ux_products");
    

    覆盖所有短代码:

    add_shortcode("ux_bestseller_products", "child_ux_products");
    add_shortcode("ux_featured_products", "child_ux_products");
    add_shortcode("ux_sale_products", "child_ux_products");
    add_shortcode("ux_latest_products", "child_ux_products");
    add_shortcode("ux_custom_products", "child_ux_products");
    add_shortcode("product_lookbook", "child_ux_products");
    add_shortcode("products_pinterest_style", "child_ux_products");
    add_shortcode("ux_products", "child_ux_products");
    

    最后,在您的 function.php 或 '/inc/init.php' 中,当 wp_loaded 时,您需要重写短代码:

    function override_shortcodes(){
      if(is_woocommerce_activated()){
        require get_stylesheet_directory() . '/inc/shortcodes/ux_products.php';
      }
    }
    add_action('wp_loaded', 'override_shortcodes', 10);
    

    【讨论】:

    • 非常适合在 Flatsome 中加载自定义短代码,谢谢。
    【解决方案2】:

    您可以尝试将其添加到您的子主题的functions.php:

    add_filter( 'template_directory', 'search_child_template_directory' );
    function search_child_template_directory( $template_dir ) {
        if (is_woocommerce_activated()) {
            return get_theme_file_path();
        }
        return $template_dir;
    }
    

    【讨论】:

    • 我收到以下信息:“您的网站出现严重错误”。我遵循相同的文件夹路径,可以吗?复制 -> 主题/flatsome/inc/shortcodes/ux_products.php 到 -> 主题/flatsome-child/inc/shortcodes/ux_products.php
    猜你喜欢
    • 2014-12-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多