【问题标题】:Customizing the Parents functions.php File in Child自定义 Child 中的 parents functions.php 文件
【发布时间】:2019-08-01 10:23:43
【问题描述】:

我现在正在自定义我的主题。标准是,登录的用户头像显示在菜单栏中,但我想在那里显示一个简单的图标。我已经编写了代码,一切正常,但代码位于 mains functions.php 文件中。因此,为了使站点可升级,我需要将代码嵌入到子主题 function.php 中。我在那里找不到任何帮助,所以也许有人可以帮助我!

非常感谢 朱利安

下面是我需要嵌入到我的子主题的functions.php文件中的代码:

add_action( 'init', 'gt3_get_page_id_from_menu' );

function gt3_user_avatar_out($avatar_size=80,$show_email=false){
    $user_id = get_current_user_id();
    $current_user = get_user_by( 'id', $user_id );
    echo "<i class='gt3_login_icon gt3_login_icon--avatar'>";
        echo '<i class="fa fa-user"></i>';
    echo "</i>";
    if ($show_email) {
        echo "<span class='gt3_login__user_email'>".esc_html( $current_user->user_email )."</span>";
    }
}

【问题讨论】:

  • 如果你已经创建了子主题,那么把它放在子的functions.php中。有什么问题吗?
  • 然后我得到一个白屏,我认为这是一个问题,因为脚本执行了两次......

标签: php wordpress customization


【解决方案1】:

如果在父主题的functions.php中声明了一个函数,在子主题中声明同名函数会导致子主题本身出现致命错误。我建议改为这样做:

父函数.php

// This can become a filter if you'll need to manipulate the current user
function gt3_get_current_user() {
    return get_user_by( 'id', get_current_user_id() );
}

add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) {
    $html .= '<i class="gt3_login_icon gt3_login_icon--avatar"><i class="fa fa-user"></i></i>';
    if( $show_email ) {
        $html .= sprintf( '<span class="gt3_login__user_email">%s</span>', esc_html( gt3_get_current_user()->user_email ) );
    }

    return $html;
}, 10, 2 );

子主题的函数.php

add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) {
    // $html here is the one that returned from the parent's theme. You can manipulate it or replace the whole output inside here. 

    return $html;
}, 10, 2 );

示例用法:

echo apply_filters( 'gt3/current_user_avatar', '', false ); // This will echo the icon without the email
echo apply_filters( 'gt3/current_user_avatar', '', true ); // This will echo the icon with the email
echo apply_filters( 'gt3/current_user_avatar', '<p>Append</p>', false ); // This will echo the icon without the email and the '<p>Append</p>' html string before everything else

结果会因您使用此过滤器的位置而异。如果您在子主题中并且您更改了添加上述过滤器的 html,则将为该主题定制结果。请记住,您可以使用use 语句在闭包中引入外部变量。例如:

$user_name = 'Mike';
add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) use ( $user_name ) {
    $html .= sprintf( '<p>Hello, %s!</p>', $user_name ); 

    return $html;
}, 10, 2 );
// This will cheer the user, and it will be place at the end of the snippet.

【讨论】:

  • 嘿,非常感谢您的解释,这正是我想要的!你们有美好的一天,感谢您的支持。
【解决方案2】:

在同一文件夹中添加新的 functions.php 文件不会覆盖您的代码,但不要从父主题的文件中复制/粘贴代码,因为它需要与您对子主题所做的任何修改分开.相反,创建一个空白文件或添加您的子主题所需的任何新的 .php 函数

【讨论】:

  • 嘿奥维斯!感谢您的回答。我已经在子主题文件夹中添加了一个 Functions.php 并且它已经可以工作,但是如果我只是编辑并复制我发布的这个特定代码,我会收到一个错误,但这个错误与代码上的问题无关,因为一切正常如果我在主函数文件中输入代码。因此,如果我在 Childs functions.php 中插入代码,我必须对我的站点运行的代码进行修改。谢谢 Julian
  • 云你请解释一下如果这个工作正常那么你为什么要添加到主function.php中?
  • 我想我理解你的问题不正确,但我需要在 Childs function.php 中实现代码,因为主题更新这是你的问题吗?
猜你喜欢
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多