wordpress 是开源软件 – 用户可以按照自己的意愿安装、修改和分发它。由于每个人都可以访问源代码,数百万 wordpress 专家和开发人员可以创建工具和扩展并与公众分享。
让我们看看如何将 css 和 js 文件加入到你的wordpress项目中。
大多数新开发者都喜欢,
里面“header.php”
<head> <title><?php echo get_bloginfo(); ?></title> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="<?php echo get_template_directory_uri(); ?>/assets/css/bootstrap.min.css" rel="stylesheet"> <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet"> <link href="<?php echo get_template_directory_uri(); ?>/assets/css/custom.css" rel="stylesheet" type="text/css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="<?php echo get_template_directory_uri(); ?>/assets/js/bootstrap.bundle.min.js"></script> <script src="<?php echo get_template_directory_uri(); ?>/assets/js/font-awesome-all.min.js"></script> <script src="<?php echo get_template_directory_uri(); ?>/assets/js/custom.js"></script> </head>
内部“footer.php”
<footer> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="<?php echo get_template_directory_uri(); ?>/assets/js/bootstrap.bundle.min.js"></script> <script src="<?php echo get_template_directory_uri(); ?>/assets/js/font-awesome-all.min.js"></script> <script src="<?php echo get_template_directory_uri(); ?>/assets/js/custom.js"></script> </footer>
但是,这不是在 wordpress 项目中对 css 和 js 文件进行排队的正确方法。为了对接它,首先转到“functions.php”文件并像这样将文件排入队列。让我们看看西格玛技巧......
第1步:functions.php
function my_theme_enqueue_styles_scripts() { // enqueue css files wp_enqueue_style('bootstrap-css', get_template_directory_uri() . '/assets/css/bootstrap.min.css'); wp_enqueue_style('aos-css', 'https://unpkg.com/aos@2.3.1/dist/aos.css'); wp_enqueue_style('custom-css', get_template_directory_uri() . '/assets/css/custom.css'); // enqueue default jquery in wordpress. wp_enqueue_script('jquery'); wp_enqueue_script('bootstrap-js', get_template_directory_uri() . '/assets/js/bootstrap.bundle.min.js', array('jquery'), null, true); wp_enqueue_script('font-awesome-js', get_template_directory_uri() . '/assets/js/font-awesome-all.min.js', array(), null, true); wp_enqueue_script('custom-js', get_template_directory_uri() . '/assets/js/custom.js', array('jquery'), null, true); } add_action('wp_enqueue_scripts', 'my_theme_enqueue_styles_scripts');
注意:wordpress提供了未压缩版本的jquery。所以我们可以简单地在所需的 js 文件中使用该 jquery !为此,您只需在“wp_eneueue_script”中传递array('jquery')参数即可。要记住的一件事是 jquery 有 2 个主要版本:未压缩和压缩。在未压缩版本中,ajax 将无法正常工作。
第2步:现在在“header.php”中
现在,我们需要使用 wp_head();在
<head> <title><?php echo get_bloginfo(); ?></title> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <?php wp_head(); ?> </head>
第3步:现在进入“footer.php”
现在,我们需要使用 wp_footer();在标签下
<?php wp_footer(); ?> </body>
在您的 wordpress 主题中包含 wp_head() 和 wp_footer() 至关重要。这些功能会自动插入wordpress、主题和插件所需的基本脚本、样式和元数据,确保适当的功能、兼容性和性能。 wp_head() 在 seo 的 部分添加必要的元素,而 wp_footer() 在末尾包含脚本以推迟非关键 javascript,提高页面加载速度。这些功能对于插件集成和动态主题定制以及分析和跟踪代码的正确放置至关重要。