简体   繁体   English

在Wordpress上为每个页面自定义CSS和JS

[英]Custom CSS and JS for each page on Wordpress

I'm doing a website using wordpress. 我正在使用wordpress建立网站。 It's my first project using it. 这是我第一个使用它的项目。 I'm looking for a way to create custom css for each page of my website because my pages are extremely different. 我正在寻找一种为网站的每个页面创建自定义CSS的方法,因为我的页面截然不同。 I don't want to put all in the styles.css of my theme. 我不想将所有内容放入主题的styles.css中。 I know I can do something like: 我知道我可以做类似的事情:

function initScriptsAndStyles()
{
    if ( is_post( 'home' ) )
    {
        wp_enqueue_style( 'style', get_template_directory_uri() . '/css/home.css');
    }
}
add_action( 'wp_enqueue_scripts', 'initScriptsAndStyles' );

But I don't want to do this for each post also, you will always be worried that no body changes the name or something... I found people that says it can be inserted dynamically, but i don't know the way to do that, didn't found any tutorial that explain step by step. 但是我也不想在每个帖子中都这样,您总是会担心没有主体会更改名称或其他内容。我发现有人说可以动态插入它,但是我不知道该怎么做。这样做,没有找到任何可以逐步解释的教程。

Anyone can help me? 有人可以帮助我吗?

Cheers! 干杯!

You can create different headers, different footers and totally different style and JS files and include them just wherever you need it. 您可以创建不同的页眉,页脚,完全不同的样式和JS文件,并在需要的地方包括它们。 You can create templates for each page if they look different. 您可以为每个页面创建模板(如果它们看起来不同)。

In your theme directory, create an empty php page , page-home.php. 在主题目录中,创建一个空的php页面page-home.php。

In the beginning of the page, write the following: 在页面的开头,编写以下内容:

<?php
/**
 * Template Name: My Custom Home Pgae
 */

 get_header();
?>

Then write all your code in HTML / PHP 然后用HTML / PHP编写所有代码

In the end, include footer like this: 最后,包括这样的页脚:

<?php get_footer(); ?>

Now go to your wordpress dashboard and click on Pages. 现在转到您的wordpress仪表板,然后单击Pages。 Add a new page and on the right hand side, (near the publish button), you will see an option to assign page a template. 添加一个新页面,并在右侧(“发布”按钮附近),您会看到一个为页面分配模板的选项。 You will see "Test" there because you assigned that template name to your page-home.php 您将在此处看到“测试”,因为您已将该模板名称分配给page-home.php

Assign you page that template and your page will reflect the code you assigned. 为您的页面分配该模板,您的页面将反映您分配的代码。

For CSS, you can even get rid of entire style.css 对于CSS,您甚至可以摆脱整个style.css

You can create a file or folder to keep your css files in your theme folder and include it in the header.php file that comes with wordpress. 您可以创建一个文件或文件夹,以将css文件保留在主题文件夹中,并将其包含在wordpress随附的header.php文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM