如何在子主题的 functions.php 中覆盖父主题的 add_image_size 和 $content_width
How to override a parent theme's add_image_size and $content_width in the child theme's functions.php
我正在尝试覆盖父主题中的某些功能,
我想将 add_image_size
从 590 覆盖到 800,将 $content-width
从 590 覆盖到 850。
这里是父主题的functions.php。
class WPEX_Theme_Class {
public function __construct() {
// Theme setup: Adds theme-support, image sizes, menus, etc.
add_action( 'after_setup_theme', array( &$this, 'setup' ), 10 );
}
public function setup() {
// Set content width variable
global $content_width;
if ( ! isset( $content_width ) ) {
$content_width = 590;
}
// Add theme support
add_theme_support( 'post-thumbnails' );
// Add image sizes
add_image_size( 'wpex-entry', 590, 9999, false );
add_image_size( 'wpex-post', 590, 9999, false );
}
$blogger_theme_setup = new WPEX_Theme_Class;
}
这是我的尝试:
function __construct()
{
add_action('after_setup_theme', array($this, 'change_theme'));
}
function change_theme()
{
remove_action('add_image_size', 'setup');
add_action('wpex-post', array($this, 'setup'));
}
function setup() {
add_theme_support( 'post-thumbnails' );
// Add image sizes
add_image_size( 'wpex-entry', 800, 9999, false );
add_image_size( 'wpex-post', 800, 9999, false );
}
它不起作用,我需要更改什么?
将以下代码添加到您的子主题将重新声明图像大小和 content_width。
您不需要删除由父主题设置的图像尺寸 - 您可以通过调用 add_image_size
函数来覆盖它们 在 父主题调用它之后.
我们可以通过为 add_action
使用较低的优先级来做到这一点。父主题使用 10
所以我们可以使用 11
.
// use priority 11 to hook into after_setup_theme AFTER the parent theme
add_action('after_setup_theme', 'reset_parent_setup', 11);
function reset_parent_setup()
{
// Override the image sizes
add_image_size( 'wpex-entry', 800, 9999, false );
add_image_size( 'wpex-post', 800, 9999, false );
// Set content width variable
global $content_width;
$content_width = 850;
}
注:
add_image size
不会自动创建已上传图像的新版本,因此 之后不要忘记重新生成图像!
您可能还需要清除任何可能影响它的缓存。
更新:检查是否注册了正确的尺寸
下面的函数将打印两种图像尺寸 wpex-entry 和 wpex-post 的尺寸。将此添加到您的 functions.php 以检查它们是什么(注意:die()
函数将停止显示页面的其余部分,以便更容易看到正在显示的值):
add_action('loop_start', 'debug_image_sizes');
function debug_image_sizes() {
global $_wp_additional_image_sizes;
if ( isset( $_wp_additional_image_sizes['wpex-entry'] ) ){
echo '<p>wpex-entry Image Size: </p><pre>';
var_dump( $_wp_additional_image_sizes['wpex-entry'] );
echo '</pre>';
}
else echo "<p>wpex-entry Image Size not found!!</p>";
if ( isset( $_wp_additional_image_sizes['wpex-post'] ) ){
echo '<p>wpex-post Image Size: </p><pre>';
var_dump( $_wp_additional_image_sizes['wpex-post'] );
echo '</pre>';
}
else echo "<p>wpex-post Image Size not found!!</p>";
die();
}
我正在尝试覆盖父主题中的某些功能,
我想将 add_image_size
从 590 覆盖到 800,将 $content-width
从 590 覆盖到 850。
这里是父主题的functions.php。
class WPEX_Theme_Class {
public function __construct() {
// Theme setup: Adds theme-support, image sizes, menus, etc.
add_action( 'after_setup_theme', array( &$this, 'setup' ), 10 );
}
public function setup() {
// Set content width variable
global $content_width;
if ( ! isset( $content_width ) ) {
$content_width = 590;
}
// Add theme support
add_theme_support( 'post-thumbnails' );
// Add image sizes
add_image_size( 'wpex-entry', 590, 9999, false );
add_image_size( 'wpex-post', 590, 9999, false );
}
$blogger_theme_setup = new WPEX_Theme_Class;
}
这是我的尝试:
function __construct()
{
add_action('after_setup_theme', array($this, 'change_theme'));
}
function change_theme()
{
remove_action('add_image_size', 'setup');
add_action('wpex-post', array($this, 'setup'));
}
function setup() {
add_theme_support( 'post-thumbnails' );
// Add image sizes
add_image_size( 'wpex-entry', 800, 9999, false );
add_image_size( 'wpex-post', 800, 9999, false );
}
它不起作用,我需要更改什么?
将以下代码添加到您的子主题将重新声明图像大小和 content_width。
您不需要删除由父主题设置的图像尺寸 - 您可以通过调用 add_image_size
函数来覆盖它们 在 父主题调用它之后.
我们可以通过为 add_action
使用较低的优先级来做到这一点。父主题使用 10
所以我们可以使用 11
.
// use priority 11 to hook into after_setup_theme AFTER the parent theme
add_action('after_setup_theme', 'reset_parent_setup', 11);
function reset_parent_setup()
{
// Override the image sizes
add_image_size( 'wpex-entry', 800, 9999, false );
add_image_size( 'wpex-post', 800, 9999, false );
// Set content width variable
global $content_width;
$content_width = 850;
}
注:
add_image size
不会自动创建已上传图像的新版本,因此 之后不要忘记重新生成图像!
您可能还需要清除任何可能影响它的缓存。
更新:检查是否注册了正确的尺寸
下面的函数将打印两种图像尺寸 wpex-entry 和 wpex-post 的尺寸。将此添加到您的 functions.php 以检查它们是什么(注意:die()
函数将停止显示页面的其余部分,以便更容易看到正在显示的值):
add_action('loop_start', 'debug_image_sizes');
function debug_image_sizes() {
global $_wp_additional_image_sizes;
if ( isset( $_wp_additional_image_sizes['wpex-entry'] ) ){
echo '<p>wpex-entry Image Size: </p><pre>';
var_dump( $_wp_additional_image_sizes['wpex-entry'] );
echo '</pre>';
}
else echo "<p>wpex-entry Image Size not found!!</p>";
if ( isset( $_wp_additional_image_sizes['wpex-post'] ) ){
echo '<p>wpex-post Image Size: </p><pre>';
var_dump( $_wp_additional_image_sizes['wpex-post'] );
echo '</pre>';
}
else echo "<p>wpex-post Image Size not found!!</p>";
die();
}