简体   繁体   English

在Wordpress中链接样式表

[英]Linking stylesheet in Wordpress

I am trying to link my stylesheet to another page in Wordpress. 我正在尝试将样式表链接到Wordpress中的另一个页面。 The actual Wordpress installation is within a folder, within the actual site. 实际的Wordpress安装位于实际站点内的文件夹中。 It's set up this way because I only want to use WP for a specific section of the site (it was an afterthought, I know this is isn't necessarily the "correct" way to do things...) 之所以这样设置,是因为我只想在网站的特定部分使用WP(这是事后的想法,我知道这不一定是“正确”的做事方式...)

I have the front page set up and the styles are all working fine. 我已经设置好首页,并且所有样式都工作正常。 But when a create a new page and try to use get_header to pull in the styles, they don't work. 但是,当创建一个新页面并尝试使用get_header插入样式时,它们将不起作用。 The browser is looking for a page called styles.css, not a stylesheet. 浏览器正在寻找一个名为styles.css的页面,而不是样式表。

I've tried to use "enqueue" in the functions.php file, but it still won't work. 我尝试在functions.php文件中使用“入队”,但仍然无法正常工作。 I have a copy of my style sheet in the theme folder and also one inside a css folder. 我在主题文件夹中有一份样式表,在css文件夹中也有一份。

Example of using enqueue for the copy inside the css folder: 在css文件夹内的副本中使用enqueue的示例:

wp_enqueue_script( 'styles', 'get_stylesheet_directory_uri()' . 'css/styles2.css' );

*I am using get_header in my page template file, (same header as the front page which is working fine), and it is linked this way: *我在我的页面模板文件中使用了get_header(与正常工作的首页相同的标题),并且通过以下方式链接:

<link rel="stylesheet" type="text/css" href="../css/styles2.css">

I'm pretty sure the issue is the "../" but when I substitute echo get_stylesheet_directory_uri()....... instead of the ../, it doesn't work as it should. 我很确定问题出在“ ../”,但是当我用echo get_stylesheet_directory_uri().......代替../时,它无法正常工作。

Any help would be great as I'm newer to WP development. 如果我是WP开发的新手,那么任何帮助都将非常有用。

Thanks everyone 感谢大家

您必须这样写才能链接模板样式表...

 wp_enqueue_script( 'styles', get_template_directory_uri(). 'css/styles2.css',  array(), '0.0.1' );

Add Style sheet like this: 像这样添加样式表:

wp_enqueue_style( 'styles', bloginfo('template_url').'/css/styles2.css' );

You can view more detail at here 您可以在这里查看更多详细信息

You need to hook the css: If you are using child theme then hook like: 您需要钩住CSS:如果您使用的是子主题,则应如下钩子:

add_action( 'wp_enqueue_scripts', 'enqueue_unique_function_name_here', 0);
function enqueue_unique_function_name_here()
{
    wp_enqueue_style( 'css_unique_handle_name_here', get_template_directory_uri(). 'folder_path_inside_child_theme/style_sheet_file_name_here.css',  array(), '0.0.1' );
}

If you are using parent theme (no child theme) then hook like: 如果您使用的是父主题(没有子主题),则可以像这样进行钩子:

add_action( 'wp_enqueue_scripts', 'enqueue_unique_function_name_here', 0);
function enqueue_unique_function_name_here()
{
    wp_enqueue_style( 'css_unique_handle_name_here', get_stylesheet_directory_uri(). 'folder_path_inside_child_theme/style_sheet_file_name_here.css',  array(), '0.0.1' );
}

If want to enqueue in admin side then just change hook name "wp_enqueue_scripts" to "admin_enqueue_scripts". 如果要排队进入管理端,则只需将钩子名称“ wp_enqueue_scripts”更改为“ admin_enqueue_scripts”即可。

Try now. 现在试试。

You have used wp_enqueue_script() instead of wp_enqueue_style() 您已使用wp_enqueue_script()而不是wp_enqueue_style()

wp_enqueue_style used for Enqueue Style wp_enqueue_style用于入队样式
wp_enqueue_script used for Enqueue Script wp_enqueue_script用于入队脚本

wp_enqueue_style( 'styles', 'get_stylesheet_directory_uri()' . 'css/styles2.css' );

Here is the full example for the same. 这是相同的完整示例。

add_action( 'wp_enqueue_scripts', 'enqueue_custom_style');
function enqueue_custom_style()
{
    wp_enqueue_style( 'styles', 'get_stylesheet_directory_uri()' . 'css/styles2.css' );
}

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

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