What's the difference between get_stylesheet_directory_uri() and get_template_directory_uri() when enqueuing scripts in wordpress - wordpress

It's not a serious question.
I normally use get_stylesheet_directory_uri() when enqueuing scripts in WordPress, which worked fine so far.
I just wonder what's the difference between get_stylesheet_directory_uri() and get_template_directory_uri() when enqueuing scripts in wordpress.
According to WordPress Codex:
get_template_directory_uri -> Retrieve template directory URI for the
current theme.
get_stylesheet_directory_uri ->Retrieve stylesheet directory URI for
the current theme/child theme
Then, get_template_directory_uri cannot be used for a child theme?

Both functions can be used in a parent or a child theme.
get_template_directory_uri will always refer to the parent theme folder for assets.
get_stylesheet_directory_uri will refer to the "current" theme folder for assets (which could be the parent or the child, depending on where it is called).
For example, in a child theme:
// This will point to style.css in child theme
wp_enqueue_style( 'my_child_styles', get_stylesheet_directory_uri().'/style.css' );
// This will point to style.css in the parent theme
wp_enqueue_style( 'my_parent_styles', get_template_directory_uri().'/style.css' );
Note that if a theme is not a child theme, then it is considered a parent theme.

Related

How properly enqueue a child theme

I've been trying to develop a simple child theme to tweak a few things on my website which currently uses the vantage theme. So far, I've had little success.
The two files (style.css and function.php) I've created only have a few lines of code, but I'm still unable to pinpoint the problem.
<?php
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles',
PHP_INT_MAX);
function enqueue_child_theme_styles() {
wp_enqueue_style( 'parent-style',
get_template_directory_uri().'/style.css' );
}
?>
/*
Theme Name: vantage-child
Template: Vantage
*/
body {
color:aqua;
}
I suspect the problem is with the enqueueing in the PHP file, but all configurations that I have found on the internet and the wordpress codex don't seem to work on my site. When I activate the child theme created by my code, the site reverts to ONLY the styles in my child theme's stylesheet, instead of falling back on the parent theme's stylesheet when no styles are specified in the child theme.
For child theme you can first need to create folder with themename-child.
Then you have to create style.css file.
Put the following lines in your style.css
/*
Theme Name: Twenty Fourteen Child
Theme URI: http://yourwebsite.com/twentyfourteen-child/
Description: My first child theme, based on Twenty Fourteen
Author: Joshi Niket
Template: twentyfourteen
Text Domain: twenty-fourteen-child
*/
Then create functions.php in child theme folder and put the following code.
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
?>
Please check following link to make child theme with proper steps, i have added few here to guid you.ReferenceLink
Thank you ,I hope this helps you.
Please install and create child theme by using One-Click Child Theme
plugin. This is very easy to use

Returning the correct path for a wordpress CHILD theme

I know wordpress quite well, but this is the first time that I am creating a child theme. I realized that I need to link everything differently in a child theme.
I got it to work for images by using "stylesheet_directly", like so:
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/icon.png" />
However, I am now trying to enqueue a javascript file in the child theme's functions.php I tried using "stylesheet_directory" again, but it returned a path of the parent's theme, and not the child theme.
Why does "stylesheet_directory" work for the image files in my child theme, but not for enqueing javascript files in my functins file?
Here is what I have in my functions. What else can I use to return the correct path?
wp_enqueue_script( 'ga-slider', bloginfo('stylesheet_directory') . '/js/ga-slider.js');
I have used get_stylesheet_directory_uri() instead of yours bloginfo('stylesheet_directory') . Ex.
wp_enqueue_script('script-name', get_stylesheet_directory_uri() . '/js/script.js', array('jquery'),2, true );
you can use get_stylesheet_directory_uri() for relative path to the child theme directory.
get_stylesheet_directory() will return absolute path of the child theme directory.
Docs: https://codex.wordpress.org/Function_Reference/get_template_directory

Wordpress ?ver=### not loading the latest child-theme styles sheet

I have WordPress theme with a child theme. After editing and saving the child style sheet, I don't see the changes. Looking at the source code, the style sheet is loaded but has this ver at the end: style.css?ver=4.9.7
Now, the styles show when I load style.css but shows an empty file with comments (the original file when first got the theme) style.css?ver=4.9.7
I understand that it is encouraged to have custom styles in the child theme but, then why doesn't WordPress support this?
Any advice or help is appreciated. Thanks.
This version added on function.php file, check it first
Better enqueue style on function.php like this
function child_themes_styles() {
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array(), wp_get_theme()->get('Version'));
}
add_action( 'wp_enqueue_scripts', 'child_themes_styles' );

Child theme not including template files of the parent in WordPress

I am creating a child theme , but my child theme in WordPress not including template files of Parent theme. I have read that all the files which are not included in Child theme are automatically imported Child theme But after activating child theme The website is displaying only header, footer and menus but not any page templates and content. Am I forgetting any step or Do I have to add some code or additional files. I am new to Wordpress.
For creating a child theme, make a folder inside your 'themes' folder with you child theme name. Now the child theme should have a style sheet in it which is a must. So add a style sheet and the beginning of the child theme style sheet should be as follows:
/*
Theme Name: (theme name) Child
Theme URI: (give URL)
Description: (give description)
Version: (give your version)
Author: (author name)
Author URI: (give URL)
Template: (name of parent theme)
*/
Of these the very important thing is the 'Template' which is the parent theme name. To avoid confusion take the name of parent theme from the parent theme style sheet.
Now the second thing is the functions of your child theme. Just add a php file with the name functions.php and place the below code
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
?>
This code is for enqueuing the parent theme styles.
Here you go! your child theme will work now. It worked for me.

style.css in css directory (i.e. not in theme root)

I'm new to WordPress and this is my first attempt to migrate a static website to WordPress. The websites layout must remain unchanged so I intend to develop a theme for it.
I'm used to having css files in the css directory, but the WordPress Theme Development page states that there has to be a style.css in the theme root.
Can I setup WP to get css/style.css instead? What is the recommended way to organise css files in WP?
You can use Wordpress' wp_enqueue_style.
function addMyScript() {
wp_enqueue_style( 'my-style', get_template_directory_uri() . '/css/my-style.css', false, '1.0', 'all' );
}
add_action('wp_head', 'addMyScript');
Just remember that you still need to have a style.css inside your theme root, even if it only has the details about the Theme in the form of comments.
Please refer to: http://codex.wordpress.org/Function_Reference/wp_enqueue_style

Resources