最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp enqueue style - Get parent theme version

programmeradmin1浏览0评论

How do I get the parent theme's version in a child theme?

I want to use it when loading the parent theme's stylesheet.

Here's how I load the stylesheets in the child theme's functions.php file:

function sometheme_enqueue_styles() {

  // Get parent theme version
  $parent_theme_version = wp_get_theme()->parent()->get( 'Version' );

  // Load parent theme stylesheet
  wp_enqueue_style( 'sometheme-style', get_template_directory_uri() . '/style.css', array(), $parent_theme_version );

  // Load child theme stylesheet
  wp_enqueue_style( 'sometheme-child-style',
    get_stylesheet_directory_uri() . '/style.css',
    array( 'sometheme-style' ),
    wp_get_theme()->get('Version')
  );

}
add_action( 'wp_enqueue_scripts', 'sometheme_enqueue_styles', 11 );

However, this will use the child theme's version for both stylesheets...

I have also tried this:

$parent_theme = wp_get_theme('sometheme');
$parent_theme_version = $parent_theme->get( 'Version' );

...and this:

$parent_theme = wp_get_theme(get_template());
$parent_theme_version = $parent_theme->get( 'Version' );

But again, the parent theme version keeps getting the version from the child theme.

Solution

It turns out that both wp_get_theme()->parent()->get( 'Version' ); and wp_get_theme()->parent()->Version; works.

The problem was that the parent theme was using $theme_version = wp_get_theme()->get( 'Version' ); as well, which means that it will use the child themes version.

Since I own the parent theme, I changed the parent theme to wp_get_theme('sometheme')->get( 'Version' ); so that it always uses its own version.

That way, I can use either wp_get_theme()->parent()->get( 'Version' ); or wp_get_theme()->parent()->Version; in the child theme.

How do I get the parent theme's version in a child theme?

I want to use it when loading the parent theme's stylesheet.

Here's how I load the stylesheets in the child theme's functions.php file:

function sometheme_enqueue_styles() {

  // Get parent theme version
  $parent_theme_version = wp_get_theme()->parent()->get( 'Version' );

  // Load parent theme stylesheet
  wp_enqueue_style( 'sometheme-style', get_template_directory_uri() . '/style.css', array(), $parent_theme_version );

  // Load child theme stylesheet
  wp_enqueue_style( 'sometheme-child-style',
    get_stylesheet_directory_uri() . '/style.css',
    array( 'sometheme-style' ),
    wp_get_theme()->get('Version')
  );

}
add_action( 'wp_enqueue_scripts', 'sometheme_enqueue_styles', 11 );

However, this will use the child theme's version for both stylesheets...

I have also tried this:

$parent_theme = wp_get_theme('sometheme');
$parent_theme_version = $parent_theme->get( 'Version' );

...and this:

$parent_theme = wp_get_theme(get_template());
$parent_theme_version = $parent_theme->get( 'Version' );

But again, the parent theme version keeps getting the version from the child theme.

Solution

It turns out that both wp_get_theme()->parent()->get( 'Version' ); and wp_get_theme()->parent()->Version; works.

The problem was that the parent theme was using $theme_version = wp_get_theme()->get( 'Version' ); as well, which means that it will use the child themes version.

Since I own the parent theme, I changed the parent theme to wp_get_theme('sometheme')->get( 'Version' ); so that it always uses its own version.

That way, I can use either wp_get_theme()->parent()->get( 'Version' ); or wp_get_theme()->parent()->Version; in the child theme.

Share Improve this question edited Dec 19, 2020 at 15:51 transbetacism asked Dec 18, 2020 at 14:39 transbetacismtransbetacism 2032 silver badges7 bronze badges 1
  • Are you validating that your $parent_theme assignment is actually getting the parent? See below for one option to test this. – jdm2112 Commented Dec 18, 2020 at 15:44
Add a comment  | 

2 Answers 2

Reset to default 5

In the WP_Theme class, the get method gives you a sanitized theme header. You cannot use it to extract a property. Actually you don't need to, as you can access it directly like this:

 // get the parent object
 $parent = wp_get_theme()->parent();
 // get parent version
 if (!empty($parent)) $parent_version = $parent->Version;

Seeing your edit based on the suggested method from cjbj, make sure you are using the actual directory name for the parent theme when calling wp_get_theme(). Note - this is not the slug or written name of the theme in many cases.

If sometheme is the file directory for the parent theme, then this should return the parent theme object:

var_dump( wp_get_theme( 'sometheme' ) );

Within that theme object is the version. Let's make sure you are getting the parent theme returned first.

Add your output for that var_dump() statement to your question if it is not the parent theme.

发布评论

评论列表(0)

  1. 暂无评论