首页 问答 正文

WordPress如何在前端页面添加CSS?

注册会员 / 悠然自学 / 2023-06-12/ 浏览 135 次

在WordPress的前端页面中添加CSS有多种方法,以下是几种常用的方法示例:

  1. 内联样式表:可以在页面的 <head> 标签内使用 <style> 标签来添加内联样式。例如:
<head>
    <style>
        body {
            background-color: #f2f2f2;
            font-family: Arial, sans-serif;
        }
    </style>
</head> 
  1. 外部样式表:可以通过在主题的 functions.php 文件中使用 wp_enqueue_style 函数来加载外部样式表。首先在主题文件夹下创建一个新的CSS文件,并在 functions.php 文件中添加以下代码:
function enqueue_custom_styles() {
    wp_enqueue_style( 'custom-style', get_stylesheet_directory_uri() . '/custom.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_styles' ); 
  1. 内联样式:可以直接在模板文件中添加CSS样式。例如,在 header.php 文件中:
<header style="background-color: #f2f2f2; padding: 20px;">
    <h1>Welcome to my website</h1>
</header> 

以上是WordPress中在前端页面中添加CSS的几种常见方法示例。根据实际情况和个人喜好选择适合的方式来添加CSS样式。

大家谈论
    我的见解