覆盖主题功能

时间:2016-10-19 17:11:36

标签: php wordpress woocommerce

在我的主题中有一个带有很多功能的functions-template.php文件。其中一个回应了网站上的类别描述。

 function woocommerce_taxonomy_archive_description() {
if ( is_tax( array( 'product_cat', 'product_tag' ) ) && get_query_var( 'paged' ) == 0 ) {
    global $wp_query;

    $cat          = $wp_query->get_queried_object();
    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
    $image        = wp_get_attachment_image_src( $thumbnail_id, 'full' );

    $description = apply_filters( 'the_content', term_description() );

    if ( $image && yit_get_option( 'shop-category-image' ) == 1 ) {
        echo '<div class="term-header-image"><img src="' . $image[0] . '" width="' . $image[1] . '" height="' . $image[1] . '" alt="' . $cat->name . '" /></div>';
    }

    if ( $description ) {
        echo '<div class="term-description">' . $description . '</div>';
    }
}
}

我希望在不弄乱文件的情况下回显另一个变量。有没有办法超越&#34;现有的功能?

我一直在用mu-plugins等摆弄,但没有成功。 在我的自定义函数文件中添加相同的函数时,我总是遇到Fatal error: Cannot redeclare woocommerce_taxonomy_archive_description() (previously declared in错误。

1 个答案:

答案 0 :(得分:3)

是的,它可以从子主题中覆盖。您可以从子主题functions.php文件中覆盖该函数。

详细了解儿童主题https://codex.wordpress.org/Child_Themes

WordPress首先加载子主题,然后加载父主题。因此,如果您在子主题中创建一个具有相同名称的函数,那么! function_exists的if条件将为false,因此不会声明此函数。

如果要从插件中覆盖,则必须在先前的执行中声明该函数。尝试在init钩子中声明它。

    add_action('init', 'theme_func_override', 5);
    function theme_func_override(){
        function override_func(){
            //code goes here
        }
    } 

<强>更新

如果函数未在if条件下使用function_exists()进行声明,则您无法覆盖它!

相关问题