在Wordpress中将自定义CSS添加到页面模板 [英] Add custom css to a page template in wordpress

查看:161
本文介绍了在Wordpress中将自定义CSS添加到页面模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要一些帮助来为页面模板创建自定义的CSS文件。
关于这个问题,有很多话题,但是我阅读的每个主题我都会得到更多的信息和更多的困惑。

Hi i need some help with the creation of a custom css file for my page template. There are many topics out there regarding this issue but with each thread i read i get more information and more confused.

我为二十四个主题创建了一个子主题主题并添加了页面模板。如何将自定义CSS添加到此模板。我发现此代码
添加到了子主题的function.php中,并用我的CSS选择了合适的类。但是我该在哪里上课?我读到我必须将类添加到header.php的body标签中,但我不确定。这是正确的方法吗?

I created a child theme for the twentyfourteen theme and added a page template. How can i add custom css to this template. I discovered that this code added to the child-theme's functions.php selects the appropriate class with my css. But how and where do i put this class? I read that i have to add the class to the body tag in the header.php but i am not sure. Is this the correct way?

if (is_page_template( 'mytemplate.php' )){
$classes[] = 'myclass';
}


推荐答案

使用 is_page_template()有条件地有选择地加载CSS。

Use the is_page_template() conditional to selectively load CSS.

在下面的函数中,我们将插入 wp_enqueue_scripts 并检查是否在自定义页面模板上,以确定是否加载其他CSS。

In the function below we're hooking into wp_enqueue_scripts and checking if we're on the custom page template to determine whether to load additional CSS.

如果结果为true,我们将加载名为 css / 文件夹中的> page-template.css 。更新路径以加载正确的文件。

If the result is true we'll load a CSS file titled page-template.css from a css/ folder inside your theme. Update the path to load the correct file.

function wpse_enqueue_page_template_styles() {
    if ( is_page_template( 'mytemplate.php' ) ) {
        wp_enqueue_style( 'page-template', get_stylesheet_directory_uri() . '/css/page-template.css' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_page_template_styles' );

这篇关于在Wordpress中将自定义CSS添加到页面模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆