【问题标题】:Overwrite parent functions in child function.php WordPress覆盖子function.php WordPress中的父函数
【发布时间】:2015-05-21 12:38:39
【问题描述】:

我正在使用儿童主题。

这是父函数

function aaron_customize_css() {
    echo '<style type="text/css">';
     if ( is_admin_bar_showing() ) {
        ?>
        .main-navigation{top:32px;}
        @media screen and ( max-width: 782px ) {
            .main-navigation{top:46px;}
        }
        @media screen and ( max-width: 600px ) {
            .main-navigation{top:0px;}
        }    
    <?php
     }
    echo '.site-title{color:#' . get_header_textcolor() . ';} ';
    $header_image = get_header_image();
    if ( ! empty( $header_image ) ) {
    ?>
        .site-header {
        background: <?php esc_attr_e( get_theme_mod('aaron_header_bgcolor', '#fafafa') ) ?> url(<?php header_image(); ?>) <?php esc_attr_e( get_theme_mod('aaron_header_bgrepeat', 'no-repeat') ); ?> <?php esc_attr_e( get_theme_mod('aaron_header_bgpos', 'center top') ); ?>;
        background-size: <?php esc_attr_e( get_theme_mod('aaron_header_bgsize', 'cover') ); ?>;
        }
    <?php
    /* No image has been chosen, check for background color: */
    }else{
        if( get_theme_mod('aaron_header_bgcolor') ){
            echo '.site-header { background:' . esc_attr( get_theme_mod('aaron_header_bgcolor', '#fafafa') ) . ';}';
            echo '#action:hover, #action:focus{text-shadow:none;}';
        }
    }
    //Call to Action text color
    if( get_theme_mod( 'aaron_action_color' ) <> ' ') {
        echo '#action, #action a{ color:' . esc_attr( get_theme_mod('aaron_action_color', '#000000') ) . ';}';
    }
    echo '</style>' . "\n";
}
add_action( 'wp_head', 'aaron_customize_css');

这是已经编辑的子函数。

function aaron_customize_css_child() {
  echo "<!-- test i am in child theme   -->     ";
  echo '<style type="text/css">';
  if ( is_admin_bar_showing() ) {
    ?>
    .main-navigation{top:32px;}
    @media screen and ( max-width: 782px ) {
      .main-navigation{top:46px;}
    }
    @media screen and ( max-width: 600px ) {
      .main-navigation{top:0px;}
    }
    <?php
    }
    echo '.site-title{color:#' . get_header_textcolor() . ';} ';
    $header_image = get_header_image();
    if (! empty( $header_image)) {
      ?>
      .site-header {
        background: url(<?php header_image(); ?>) no-repeat center center;
        -webkit-background-size: cover;
        -moz-background-size:    cover;
        -o-background-size:      cover;
        background-size:         cover;
        }
    <?php
    /* No image has been chosen, check for background color: */
    }else{
        if( get_theme_mod('aaron_header_bgcolor') ){
          echo '.site-header { background:' . esc_attr( get_theme_mod('aaron_header_bgcolor', '#fafafa') ) . ';}';
          echo '#action:hover, #action:focus{text-shadow:none;}';
        }
    }
    echo '</style>' . "\n";
}
add_action( 'wp_head', 'aaron_customize_css_child');

我正在尝试覆盖 function.php 中的父函数。我按照http://code.tutsplus.com/articles/how-to-modify-the-parent-theme-behavior-within-the-child-theme--wp-31006 的说明进行操作。但它不起作用。我什至查看了有关 stackoverflow 的几个问题,但它没有我想要的东西。我确实检查了http://codex.wordpress.org/Child_Themes,它说我可以通过声明来替换子functions.php中的父函数

if(!function_exists( 'aaron_customize_css')) {
  function aaron_customize_css() {}
}

我什至尝试了许多其他方法,但我无法让它发挥作用。帮忙?

【问题讨论】:

  • 函数名称不同。您是否想在 addition 中从子级调用父函数以添加特定于子级的新功能?还是您想要完全覆盖?
  • 完全覆盖。对不起子函数,我忘了从子函数中删除“_child”。

标签: php wordpress function


【解决方案1】:

您可以使用remove_action()“删除”父主题功能。此函数移除已附加到指定动作挂钩的函数,并且通常用于将函数替换为替代函数:

remove_action('wp_head', 'aaron_customize_css');

然后添加你自己的函数来代替它:

add_action('wp_head', 'aaron_customize_css_child');

因此,总而言之,您的子主题中将包含以下内容:

function aaron_customize_css_child() {
    remove_action('wp_head', 'aaron_customize_css');
    // function contents
}
add_action('wp_head', 'aaron_customize_css_child', 100);

【讨论】:

  • 试过了,但它不起作用....:(应该是 add_action( 'after_setup_theme', 'remove_parent_theme_features', 10 ); function remove_parent_theme_features() { remove_action('wp_head', 'aaron_customize_css' ); add_action('wp_head', 'aaron_customize_css_child'); } 但最后一句应该是 add_action('wp_head', 'aaron_customize_css'); 没有“_child”这个词吗?我只是想覆盖父函数或类似的东西...
  • 我什么都看不懂...但是您的子主题功能至少输出test i am in child theme吗?
  • 我刚刚编辑了我的示例,将remove_action 嵌套在子函数中
  • 如果你的函数甚至没有运行,其他事情正在发生。不管父函数是否运行,你的子函数应该是……我不想问,但是子主题是通过管理员激活的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 2021-09-11
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
相关资源
最近更新 更多