wordpress - 在子主题中覆盖函数父主题
问题描述
我有一些函数可以在我的子主题中覆盖,但继续无法重新声明 langs_content()。
我使用了很多资源进行故障排除,而一些应该直截了当的东西我无法解决
<?php
function enqueue_parent_theme_style() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style' );
function enqueue_child_theme_style() {
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri().'/style.css' );
}
//require_once( get_stylesheet_directory() . '/functions.php');
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_style', 100 );
if ( ! function_exists( 'langs_content' ) ) {
/*
* My Awesome function is awesome
*
*/
function langs_content( $atts, $content = null ) // shortcode lang
{
$lang = the_lang();
extract( shortcode_atts( array(), $atts ) );
$langs_content = '';
if(in_array($lang, $atts))
{
$langs_content .= do_shortcode($content);
}
return $langs_content;
}
add_shortcode('lang', 'langs_content');
}
?>
解决方案
真的很简单!删除短代码,init hook
然后添加新的;-)
像这样 :
/******
* Inside parent functions.php (don't touch this!)
******/
function test_shortcode() {
return "Hello World from parent theme";
}
add_shortcode('test_shortcode', 'test_shortcode');
/******
* Inside child functions.php
******/
function child_test_shortcode()
{
return "Hello World from child theme";
}
function remove_parent_theme_shortcodes()
{
remove_shortcode('test_shortcode');
// Add our new function with the same shortcode
add_shortcode('test_shortcode', 'child_test_shortcode');
}
// Remove the shortcode with init hook
add_action('init', 'remove_parent_theme_shortcodes');
推荐阅读
- r - 将块渲染为 html
- java - Android Studio Emulator 弹出而不是固定在工作室中的选项卡
- python - Statsmodel ADF 函数返回“TypeError:‘函数’对象不可下标”
- text - JMeter - 根据响应中的文本在图中有单独的线的方法
- r - 归纳树规则的覆盖范围和准确性
- codeceptjs - 如何在测试用例期间设置输入类型 datetime-local
- r - 每个 group_by 添加 n(空)行
- c# - VS C# 简单的单选按钮开关
- node.js - 安装我的私有 github 存储库时,monorepo 路径错误
- azure-devops - YAML 管道中路径过滤器中通配符的可用性