2017-08-10 79 views
0

我有以下功能在父functions.php文件坐:在儿童主题删除WordPress的功能

#-----------------------------------------------------------------# 
# Site Title 
#-----------------------------------------------------------------# 

function theme_slug_setup() { 
    add_theme_support('title-tag'); 
} 
add_action('after_setup_theme', 'theme_slug_setup'); 

if (! function_exists('_wp_render_title_tag')) { 
    function theme_slug_render_title() { ?> 

     <title><?php wp_title('|', true, 'right'); ?></title> <?php 
    } 

    add_action('wp_head', 'theme_slug_render_title'); 
} 

我想首先的禁用它,然后尝试和我的孩子函数文件将其替换...

我已经试过这个取代它:

add_action('after_setup_theme', 'theme_slug_setup'); 


    function theme_slug_render_title() { ?> 

     <title><?php wp_title('|', true, 'right'); ?></title> <?php 
    } 

    add_action('wp_head', 'theme_slug_render_title'); 

或这些将其删除:

function child_remove_parent_function() { 
    remove_action('after_setup_theme', 'theme_slug_setup'); 
} 
add_action('wp_loaded', 'child_remove_parent_function'); 


function child_remove_parent_function2() { 
    add_action('wp_head', 'theme_slug_render_title'); 
} 
add_action('wp_loaded', 'child_remove_parent_function2'); 

但我似乎无法得到任何工作!有人能够指出我要出错的地方吗?谢谢!

++++++++++++++编辑++++++++++++++

我不得不添加另一个函数内部的remove_action码 - 这工作对自己并删除标题标签...

add_action('after_setup_theme', 'remove_head_title', 0); 

function remove_head_title() { 

    remove_action('wp_head', 'theme_slug_render_title'); 
    remove_action('after_setup_theme', 'theme_slug_setup'); 
} 

回答

1

在你的子主题function.php文件需要

第一:删除操作:(你不需要包装这一行其他动作)

remove_action('after_setup_theme', 'theme_slug_setup'); 
remove_action('wp_head', 'theme_slug_render_title'); 

之后:重新声明新的功能,如果必要的:

function child_theme_slug_setup() { //Function name must be different of function in parent theme 
    add_theme_support('title-tag'); 
} 
add_action('after_setup_theme', 'child_theme_slug_setup'); 

编辑: 所以,你的孩子function.php主题的完整代码必须是:

remove_action('after_setup_theme', 'theme_slug_setup'); 
remove_action('wp_head', 'theme_slug_render_title'); 

function child_theme_slug_setup() { //Function name must be different of function in parent theme 
    add_theme_support('title-tag'); 
} 
add_action('after_setup_theme', 'child_theme_slug_setup'); 
+0

看看,所以你会更清楚地通过子函数替换父函数:https://wordpress.stackexchange.com/questions/95799/is-it-possible-to-disable-a-function-ofa-a-父主题 –

+0

谢谢Fabien - remove_action行是否需要在自己的函数中?由于这不起作用,它似乎没有任何效果...... –

+0

不,“remove_action”函数必须直接在文件中(不在其他函数中)。初始代码中的问题可能是因为您在其他操作中调用了您的'remove_action'(这太迟了)。我的编辑更清楚了吗? – Fabien

1

在此添加下面的代码儿童主题function.php文件为标题标签

 remove_action('after_setup_theme', 'theme_slug_setup'); 
     remove_action('wp_head', 'theme_slug_render_title'); 

     function child_theme_slug_setup() { 
      add_theme_support('title-tag'); 
     } 
     add_action('after_setup_theme', 'child_theme_slug_setup'); 

     if (! function_exists('_wp_render_title_tag')) { 
    function child_theme_slug_render_title() { 
?> 
<title><?php wp_title('-', true, 'right'); ?></title> 
<?php 
    } 
    add_action('wp_head', 'child_theme_slug_render_title'); 
} 
+0

谢谢你,但它似乎并没有工作 - 它似乎没有任何效果,标题标签仍然存在(即使我用我自己的代码甚至简单的文本替换wp_title部分... –

+0

此外,请确保以下内容不在您的header.php模板中: <?php wp_title('|',true,'right');?>

+0

谢谢 - 但是,它仍然没有工作,如果你看看编辑我已经做了原始问题,我添加它在另一个功能,它似乎工作 - 我只需要现在结合这个代码的其余部分来替换标题? –