简体   繁体   English

将wordpress中的样式表和脚本与functions.php链接

[英]linking stylesheets and scripts in wordpress with functions.php

I am following along with this tutorial. 我将跟随教程。

I am trying to link the javascript and css files that are usually linked in the header using wp_enqueue_scripts in functions.php . 我想这是通常使用的标题链接的JavaScript和CSS文件链接wp_enqueue_scriptsfunctions.php With what I have now, only a blank white page is loading. 现在,只有空白的页面正在加载。

functions.php functions.php

<?php
// Add scripts and stylesheets
function blogtheme_scripts() {
    wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css', array() );
    wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), '3.3.6' );
    wp_enqueue_style( 'blog', get_template_directory_uri() . '/css/blog.css' );
    wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '3.3.6', true );
    wp_register_style('OpenSans', 'http://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800');
    wp_enqueue_style( 'OpenSans');
}

add_action( 'wp_enqueue_scripts', 'blogtheme_scripts' );

// WordPress Titles
add_theme_support( 'title-tag' );

?>

header.php header.php

I commented out the html links because I figured they weren't supposed to be in there once I linked to them in functions.php : 我注释掉了html链接,因为我认为一旦我在functions.php链接到它们,就不应该将它们放在其中:

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  <meta name="description" content="">
  <meta name="author" content="">
  <!-- Bootstrap core CSS -->
  <!--<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">-->

  <!-- Custom styles for this template -->
  <!--<link href="<?php bloginfo(//'template_directory');?>/blog.css" rel="stylesheet">-->

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
  <?php wp_head();?>
</head>

The page just loads as a white page, I'm assuming it's because something is wrong with the linking, and I don't see any errors in the console. 该页面仅作为白色页面加载,我认为这是因为链接有问题,并且在控制台中没有看到任何错误。

Could it be an issue with the versions of the scripts - 3.3.6 ? 脚本版本3.3.6可能有问题吗?

If the tutorial says to put a call to wp_head() before the closing body tag, then the tutorial is wrong. 如果教程说要在结束body标签之前调用wp_head() ,则该教程是错误的。 It should be before the closing head tag. 它应该是闭幕前head标记。

wp_head() is the function that loads your head template and should be located within the <head></head> tags in your template. wp_head()是加载您的head模板的函数,应位于模板的<head></head>标记内。 The best place to put it is just before the </head> closing tag. 放置它的最佳位置是</head>结束标记之前。

The wp_print_styles should not be used anymore to enqueue styles (refer here ). wp_print_styles不应再用于排队样式(请参阅此处 )。 You can add your google fonts enqueuing to your blogtheme_scripts function. 您可以将排队的Google字体添加到blogtheme_scripts函数。 I bet that will work :) 我敢打赌,这会起作用的

I enabled debugging in wp-config.php as suggested by an answer to this question on the wordpress site: 我在wordpress网站上针对此问题的答案建议在wp-config.php启用调试:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );

After doing this I saw that I just had a syntax error - it worked after I fixed it. 完成此操作后,我发现我只是遇到语法错误-在修复它后,它可以工作。

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

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