the*_*ter 18 css php wordpress
嗨,我需要一些帮助,为我的页面模板创建自定义css文件.关于这个问题有很多主题,但我阅读的每个帖子都会得到更多信息和更多信息.
我为二十四个主题创建了一个子主题,并添加了一个页面模板.如何将自定义css添加到此模板.我发现这个代码添加到了child-theme的functions.php中,用我的css选择了合适的类.但是我如何以及在哪里上课?我读到我必须将该类添加到header.php中的body标签,但我不确定.这是正确的方法吗?
if (is_page_template( 'mytemplate.php' )){
$classes[] = 'myclass';
}
Run Code Online (Sandbox Code Playgroud)
Nat*_*son 18
使用is_page_template()
条件有选择地加载CSS.
在下面的函数中,我们将进入wp_enqueue_scripts
并检查我们是否在自定义页面模板上以确定是否加载其他CSS.
如果结果是真的,我们会加载一个名为CSS文件page-template.css
从css/
你的主题文件夹里面.更新加载正确文件的路径.
function wpse_enqueue_page_template_styles() {
if ( is_page_template( 'mytemplate.php' ) ) {
wp_enqueue_style( 'page-template', get_template_directory_uri() . '/css/page-template.css' );
}
}
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_page_template_styles' );
Run Code Online (Sandbox Code Playgroud)
tuc*_*q88 12
这个解决方案怎么样?
<?php
function mypage_head() {
echo '<link rel="stylesheet" type="text/css" href="'.get_bloginfo('stylesheet_directory').'/includes/mypage.css">'."\n"
}
add_action('wp_head', 'mypage_head');
?>
<?php get_header(); ?>
Run Code Online (Sandbox Code Playgroud)
您可以使用wp_head
hook将自定义内容(Javascript,CSS ..)添加到自定义模板中.我认为这种方式更好,因为所有更改都将包含在您的模板文件中,因此您无需在其他位置签入.
我从以下网址获得此解决方案:http://scratch99.com/wordpress/development/custom-page-template-external-css-file/.
归档时间: |
|
查看次数: |
16022 次 |
最近记录: |