【问题标题】:Overiding parent theme functions with a Child theme in WordPress在 WordPress 中使用子主题覆盖父主题功能
【发布时间】:2015-01-15 18:35:16
【问题描述】:

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

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;
 }
}

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    你遇到了什么错误?

    如 WordPress 法典中所述:

    “与 style.css 不同,子主题的 functions.php 不会覆盖其父主题的对应项。相反,它会在父主题的 functions.php 之外加载。(具体而言,它会在父主题之前加载文件。)”(来源:http://codex.wordpress.org/Child_Themes

    因此,无法覆盖functions.php 文件,但您可以将自己的函数添加到子主题中的functions.php 文件中。一定要给自己的函数加上前缀,这样就不会和父主题中的函数冲突了。

    【讨论】:

    • 问题是,我正在尝试删除 ' | ' 来自发布的元数据。这就是我需要做的,但它包含在主题的功能中: if ( in_array( 'date', $postinfo ) ) $postinfo_meta .= ' | ' . esc_html__('on',$themename) 。 ' ' 。 get_the_time($date_format);
    • 另外@Sjors,我尝试从父主题中删除该功能并用更新的主题替换它。
    • 从父项中删除该功能并将其放置在您的子主题中没有帮助吗?你确定这是正确的功能吗?
    • 顺便说一句,如果你没有从父主题中删除原始函数,下面的代码将不会被执行,因为函数已经存在: if ( ! function_exists( 'cc_et_postinfo_meta' ) ) { 函数 cc_et_postinfo_meta ..
    • 我尝试使用以下方法删除该函数: function remove_et_postinfo_meta_actions() { remove_action('after_setup_theme','et_postinfo_meta',3); }
    猜你喜欢
    • 2015-07-27
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 2013-11-10
    • 2019-02-28
    • 1970-01-01
    相关资源
    最近更新 更多