Here is my code that is not working in wordpress theme. using this code trying to getting style.css file from wp root folder. what's problem in this code is any thing not correct like function name
<?php
function add_style() { wp_enqueue_style('style', get_stylesheet_uri()); }
add_action('wp_enqueue_scripts','add_style');
?>
But this code in wp function file is working correctly
register_nav_menus(array(
'primary' => __('Primary Menu')
)) ;
So can any one help me ?
<link href="<?php bloginfo('stylesheet_url');?>" rel="stylesheet">
add this to your header.php
function custom_scripts_method() {
/* CSS */
wp_enqueue_style( 'boostrap-style', get_template_directory_uri().'path to file from theme folder' );
}
add_action( 'wp_enqueue_scripts', 'custom_scripts_method' );
?>
add this in the functions.php file
and remember to use the : <?php wp_head(); ?>
in the head of your html
Related
I have been trying to get this code to work in my child theme's functions.php file in order to properly enqueue my custom CSS file to my template file/page called "dashboard". From what I understand, the functions.php file is loaded before the main query, so I am using an action hook with a callback that is shown below. But it just doesn't seem to work. Much help would be appreciated.
functions.php
add_action('get_header', function() {
if(is_page('dashboard')) {
function enqueue_style() {
wp_enqueue_style( 'dashboard-css', 'https://myurl.com/wp-content/themes/astra-child/css/dashboard.css', false );
}
add_action( 'wp_enqueue_scripts', 'enqueue_style' );
}
});
dashboard.php
<?php /*Template Name: User Dashboard*/?>
<head>
</head>
<body>
<p>this is my dashboard</p>
</body>
Try code like this in functions.php file.
function enqueue_style() {
global $wp_styles, $wp_scripts;
$protocol = is_ssl() ? 'https' : 'http';
if(is_page('dashboard')) {
// Register
wp_register_style('dashboard-css', get_stylesheet_directory_uri() . '/css/dashboard.css');
// Enqueue
wp_enqueue_style('dashboard-css');
}
}
add_action('wp_enqueue_scripts', 'enqueue_style');
I'd like to enqueue a stylesheet called charts.css & charts.min.css.
I'm not sure, why it's not working.
That's my added code in functions.php Wordpress:
function additional_stylesheets() {
wp_register_style( 'custom01', get_template_directory_uri().'/assets/css/minified/charts.min.css' );
wp_enqueue_style( 'custom01' );
wp_register_style( 'custom02', get_template_directory_uri().'/assets/css/unminified/charts.css' );
wp_enqueue_style( 'custom02' );
}
add_action( 'wp_enqueue_scripts', 'additional_stylesheets' );
I tried in elementor to style a table but nothin' happened.
So I also tried to use:
<link rel="stylesheet" href="wp-content/themes/astra/assets/css/minified/charts.min.css">
but that's also not working.
Did I make a mistake somewhere?
I use the theme astra in WordPress.
The path is wp-content/themes/astra/.
After appending styleheets in WordPress I do not have to call them in html code anymore am I right?
You don't need to register them. Instead use the following code:
function additional_stylesheets() {
wp_enqueue_style('charts_min_styles', get_theme_file_uri('/assets/css/minified/charts.min.css'), NULL, 1.2, false);
wp_enqueue_style('charts_styles', get_theme_file_uri('/assets/css/unminified/charts.css'), NULL, 1.2, false);
}
add_action( 'wp_enqueue_scripts', 'additional_stylesheets' );
The code goes to the functions.php of your active theme or child them.
I'm trying to migrate my HTML to WordPress template. While I'm doing that, I want to keep my CSS files where they are placed and import them to the code. I found out that I can use functions.php file to make it easier.
Below is the code that I added to functions.php:
<?php
function add_theme_codes() {
wp_enqueue_style(‘bootstrap’,get_stylesheet_directory_uri().’/css/bootstrap_grid.css’, ‘all’);
wp_enqueue_style( ‘style’, get_stylesheet_directory_uri().’/css/main.css’, ‘all’);
}
add_action( ‘wp_enqueue_scripts’, ‘add_theme_codes’ );
?>
But still my WordPress site is not importing the CSS files.
Still you have selected the answer I would like to answer on your last desire that you want this to work in a proper way that WordPress do. Please check wp_footer is called before body tag closed or not. Action Hook wp_enqueue_scripts will called in wp_footer function. Please try once and let me know the result.
You can get css file from below code:
<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style') );
}
?>
Or in Header section before wp_head() add below code
<link href="<?php echo get_bloginfo('stylesheet_directory');?>/css/bootstrap_grid.css" rel="stylesheet" />
<link href="<?php echo get_bloginfo('stylesheet_directory');?>/css/main.css" rel="stylesheet" />
Wrap with add_action and this might work
<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_register_style('css-ui',get_template_directory_uri().'/assets/css/style.css' );
wp_enqueue_style('css-ui' ); }
I've started building a theme for Wordpress. Add a certain point I want to add some style to the site. I can get it to work when adding the style to the header, but I want to use the function.php file to accomplish this. This is what I got:
<?php
function addCss(){
wp_enqueue_style('style', get_template_directory_uri() . 'style.css');
}
add_action("wp_enqueue_scripts", "addCss");
?>
It's a very simple function, but for some reason, the function is not fired. I also don't get any errors and style.css is not added to the site according to the console.
My file structure:
I've tried searching for a solution, but couldn't find any. I hope u guys can help me.
Make sure you call <?php wp_head(); ?> in header.php just before ending </head> tag.
function addMyStyle() {
wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css', false, '1.0', 'all' );
}
add_action('wp_head', 'addMyStyle');
get_template_directory_uri() return a path without the "/" at the end.
Add a "/" in your path:
// add this line to create a versionning of your stylesheet
$ver = time();
wp_enqueue_style('style', get_template_directory_uri() . '/style.css', $ver);
I hope that help you !
You need to have wp_head() in header.php
if ( ! function_exists( 'themescripts' ) ) {
function themescripts() {
wp_enqueue_style('style', get_stylesheet_uri(), array());
wp_enqueue_script( 'jquery', array(), '', true);
....
}
add_action( 'wp_enqueue_scripts', 'themescripts' );
}
Also, make sure to have wp_footer() in footer.php file.
css conditioning if user is in homepage of wordpress?
I put this
<?php if(is_home()) {
echo '<link rel="stylesheet" href="customHome.css">'
} ?>
into my page editor and it did applied the css. but because it's using echo so there will be some bug. it echo out some thing like
> echo ' ‘ } ?>
in the end of the post.
I want to know where else should I include that? I tried to put in header but it doesn't work.
You can append css by using wp_enqueue_scripts like;
function custom_css() {
if (is_home()) {
wp_enqueue_style( 'custom_style_name', 'path_to_your_css' );
}
}
add_action( 'wp_enqueue_scripts', 'custom_css' );
Put above code block to functions.php