在 WordPress 中使用子主题覆盖父主题功能

在 WordPress 中使用子主题覆盖父主题功能

问题描述:

我试图用我在我的子主题 function.php 文件中的一个函数覆盖父主题 function.php 文件,但我遇到了一些错误.这是我到目前为止所做的..

I am trying to override the Parent theme function.php file with a function i have in my child themes function.php file but i'm getting a couple of errors. Here's what iv'e done so far..

function remove_et_postinfo_meta_actions() {

remove_action('after_setup_theme','et_postinfo_meta',3);
}

add_action('init', 'remove_et_postinfo_meta_actions');


add_action('after_setup_theme', 'cc_et_postinfo_meta', 3);


if ( ! function_exists( 'cc_et_postinfo_meta' ) ){
function cc_et_postinfo_meta( $postinfo, $date_format, $comment_zero, $comment_one, $comment_more          ){
    global $themename;

    $postinfo_meta = '';

    if ( in_array( 'author', $postinfo ) ){
        $postinfo_meta .= ' ' . esc_html__('by',$themename) . ' ' .     et_get_the_author_posts_link();
    }

    if ( in_array( 'date', $postinfo ) )
        $postinfo_meta .= ' ' . esc_html__('on',$themename) . ' ' . get_the_time( $date_format );

    if ( in_array( 'categories', $postinfo ) )
        $postinfo_meta .= ' ' . esc_html__('in',$themename) . ' ' . get_the_category_list(', ');

    if ( in_array( 'comments', $postinfo ) )
        $postinfo_meta .= ' ' . et_get_comments_popup_link( $comment_zero, $comment_one,   $comment_more );

    if ( '' != $postinfo_meta ) $postinfo_meta = __('Posted',$themename) . ' ' . $postinfo_meta;

    echo $postinfo_meta;
 }
}

您遇到了哪些错误?

如 WordPress 代码中所述:

As described in the WordPress codex:

"与 style.css 不同,子主题的functions.php 不会覆盖父主题的对应物.相反,它是在父的functions.php 之外加载的.(具体来说,它在父主题之前加载文件.)"(来源:http://codex.wordpress.org/Child_Themes)

"Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)" (source: http://codex.wordpress.org/Child_Themes)

因此,无法覆盖functions.php 文件,但您可以将自己的函数添加到子主题中的functions.php 文件中.一定要给自己的函数加上前缀,以免与父主题中的函数冲突.

So, it is not possible to override the functions.php file, but you can add your own functions to the functions.php file in your child theme. Be sure to prefix your own function so it won't conflict with the functions in the parent theme.