【发布时间】:2015-07-08 05:49:49
【问题描述】:
我想减少我的父主题生成的图像文件的数量。经过一番研究,我发现为此我需要从父主题的 php 文件 (not function.php) 中覆盖一个函数,但这是不可能的,因为 此函数不可插入(不包含在 if (!function_exists()) 条件中)。现在,我只是将父函数包装在 if (!function_exists()) 条件中,并在我的子主题 function.php 文件中覆盖它。
在所描述的情况下,是否可以覆盖父函数而不更改父主题?我在这里问是因为我没有得到开发者的任何反应。
我尝试使用子主题和插件中的下一个代码删除父主题功能,但这没有帮助:
function remove_fastnews_actions() {
remove_action('after_setup_theme','kopa_front_after_setup_theme');
}
add_action('after_setup_theme','remove_fastnews_actions');
//add_action('wp_loaded','remove_fastnews_actions');
这是我需要重写的函数(它更大,所以我只保留了我真正需要更改的内容):
add_action('after_setup_theme', 'kopa_front_after_setup_theme');
function kopa_front_after_setup_theme() {
$sizes = array(
'flexslider-image-size' => array(500, 500, TRUE, __('Flexslider Post Image (Kopatheme)', kopa_get_domain())),
'article-list-image-size' => array(300, 277, TRUE, __('Article List Post Image (Kopatheme)', kopa_get_domain())),
'article-list-sm-image-size' => array(120, 120, TRUE, __('Article List Small Post Image (Kopatheme)', kopa_get_domain())),
'article-carousel-image-size' => array(354, 354, TRUE, __('Article Carousel Post Image (Kopatheme)', kopa_get_domain())),
'entry-list-image-size' => array(227, 182, TRUE, __('Entry List Thumbnail Image (Kopatheme)', kopa_get_domain())),
'blog-image-size' => array(680, 419, TRUE, __('Blog Image Size (Kopatheme)', kopa_get_domain())),
);
apply_filters('kopa_get_image_sizes', $sizes);
foreach ($sizes as $slug => $details) {
add_image_size($slug, $details[0], $details[1], $details[2]);
}
}
相同的父函数可插入:
if( !function_exists('kopa_front_after_setup_theme') )
{
add_action('after_setup_theme', 'kopa_front_after_setup_theme');
function kopa_front_after_setup_theme() {
$sizes = array(
'flexslider-image-size' => array(500, 500, TRUE, __('Flexslider Post Image (Kopatheme)', kopa_get_domain())),
'article-list-image-size' => array(300, 277, TRUE, __('Article List Post Image (Kopatheme)', kopa_get_domain())),
'article-list-sm-image-size' => array(120, 120, TRUE, __('Article List Small Post Image (Kopatheme)', kopa_get_domain())),
'article-carousel-image-size' => array(354, 354, TRUE, __('Article Carousel Post Image (Kopatheme)', kopa_get_domain())),
'entry-list-image-size' => array(227, 182, TRUE, __('Entry List Thumbnail Image (Kopatheme)', kopa_get_domain())),
'blog-image-size' => array(680, 419, TRUE, __('Blog Image Size (Kopatheme)', kopa_get_domain())),
);
apply_filters('kopa_get_image_sizes', $sizes);
foreach ($sizes as $slug => $details) {
add_image_size($slug, $details[0], $details[1], $details[2]);
}
}
}
以及覆盖父函数的子主题函数:
add_action('after_setup_theme', 'kopa_front_after_setup_theme');
function kopa_front_after_setup_theme() {
$sizes = array(
'flexslider-image-size' => array(0, 0, TRUE, __('Flexslider Post Image (Kopatheme)', kopa_get_domain())),
'article-list-image-size' => array(0, 0, TRUE, __('Article List Post Image (Kopatheme)', kopa_get_domain())),
'article-list-sm-image-size' => array(120, 120, TRUE, __('Article List Small Post Image (Kopatheme)', kopa_get_domain())),
'article-carousel-image-size' => array(0, 0, TRUE, __('Article Carousel Post Image (Kopatheme)', kopa_get_domain())),
'entry-list-image-size' => array(0, 0, TRUE, __('Entry List Thumbnail Image (Kopatheme)', kopa_get_domain())),
'blog-image-size' => array(0, 0, TRUE, __('Blog Image Size (Kopatheme)', kopa_get_domain())),
);
apply_filters('kopa_get_image_sizes', $sizes);
foreach ($sizes as $slug => $details) {
add_image_size($slug, $details[0], $details[1], $details[2]);
}
}
【问题讨论】:
-
你可以把它放在一个插件文件中。
-
与其停止运行,您可以致电remove_image_size 删除不需要的尺寸吗?
-
@GopalSRathore - 你的意思是 remove_action 和子主题功能吗?我试过了,不行。
-
@Hobo - 也许我可以,但我不明白如何在我的上下文中执行此操作:(。
-
抱歉,误读了您的要求。看起来你应该能够使用过滤器(我之前错过了) - 查看我的答案
标签: php wordpress overriding