admin管理员组文章数量:1332865
On functions.php I have the following which works on the homepage :
function load_page_styles() {
if (is_front_page()) {
wp_register_style('home', get_template_directory_uri() . '/css/home.css', array(), 1, 'all');
wp_enqueue_style('home');
wp_register_style('font-awesome', get_template_directory_uri() . '/css/font-awesome.css', array(), 1, 'all');
wp_enqueue_style('font-awesome');
} else if (is_page( 'about')) {
wp_register_style('about', get_template_directory_uri() . '/css/about.css', array(), 1, 'all');
wp_enqueue_style('about');
}
}
add_action( 'wp_enqueue_scripts', 'load_page_styles' );
It is working fine at it showing the css file in /wp-content/themes/roots-restaurant
<link rel="stylesheet" id="home-css" href="http://localhost:8000/wp-content/themes/roots-restaurant/css/home.css?ver=1" type="text/css" media="all">
But on about us page it does not work. It shows the path as the following, which is wrong as it is targeting the wp-admin folder:
<link rel="stylesheet" id="about-css" href="http://localhost:8000/wp-admin/css/about.min.css?ver=5.4.2" type="text/css" media="all">
About Us template is in the following:
/wp-content/themes/roots-restaurant/template-about.php
Could you help me with this.
Ronny
On functions.php I have the following which works on the homepage :
function load_page_styles() {
if (is_front_page()) {
wp_register_style('home', get_template_directory_uri() . '/css/home.css', array(), 1, 'all');
wp_enqueue_style('home');
wp_register_style('font-awesome', get_template_directory_uri() . '/css/font-awesome.css', array(), 1, 'all');
wp_enqueue_style('font-awesome');
} else if (is_page( 'about')) {
wp_register_style('about', get_template_directory_uri() . '/css/about.css', array(), 1, 'all');
wp_enqueue_style('about');
}
}
add_action( 'wp_enqueue_scripts', 'load_page_styles' );
It is working fine at it showing the css file in /wp-content/themes/roots-restaurant
<link rel="stylesheet" id="home-css" href="http://localhost:8000/wp-content/themes/roots-restaurant/css/home.css?ver=1" type="text/css" media="all">
But on about us page it does not work. It shows the path as the following, which is wrong as it is targeting the wp-admin folder:
<link rel="stylesheet" id="about-css" href="http://localhost:8000/wp-admin/css/about.min.css?ver=5.4.2" type="text/css" media="all">
About Us template is in the following:
/wp-content/themes/roots-restaurant/template-about.php
Could you help me with this.
Ronny
Share Improve this question asked Jul 3, 2020 at 1:52 Rejaur RahmanRejaur Rahman 51 silver badge3 bronze badges3 Answers
Reset to default 0Do not use "about" as handle
as it seems to be not unique and used by WordPress itself. I don't see the list of style handles is documented somewhere. Anyway, use something unique like 'rejaur-about':
<?php
function load_page_styles() {
if ( is_front_page() ) {
// enqueue front page styles
} elseif ( is_page('about') ) {
wp_enqueue_style(
'rejaur-about', // the problem was the handle
get_template_directory_uri() . '/css/about.css',
array(),
1,
'all'
);
}
}
add_action( 'wp_enqueue_scripts', 'load_page_styles' );
In this particular setting the styles don't need to be registered before enqueuing them.
Off-topic: be careful with else if
and elseif
.
Update Your condition to
is_page_template( 'about.php' )
for details visit WP Official documentation
https://developer.wordpress/reference/functions/is_page/
https://developer.wordpress/reference/functions/is_page_template/#comment-497
it's shoud be working - example :
1 - index page view
2 - index sourse
3 - target page - no custom template for it
4 - target page sourse
5 - functions.php
本文标签: functionswp enqueue style on about us page
版权声明:本文标题:functions - wp enqueue style on about us page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742293052a2448173.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论