嗨 我需要一些帮助来为我的页面模板创建自定义 css 文件。 关于这个问题有很多主题,但我阅读的每个主题都会获得更多信息和更多困惑。
我为二十四主题创建了一个子主题并添加了一个页面模板。如何将自定义 CSS 添加到此模板。我发现 添加到子主题的functions.php的这段代码使用我的css选择适当的类。但是我如何以及在哪里放置这门课?我读到我必须将类添加到 header.php 中的 body 标记,但我不确定。这是正确的方法吗?
if (is_page_template( 'mytemplate.php' )){
$classes[] = 'myclass';
}
最佳答案
使用 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_stylesheet_directory_uri() . '/css/page-template.css' );
}
}
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_page_template_styles' );
关于php - 将自定义 css 添加到 wordpress 中的页面模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28249774/