Файл Wordpress functions.php не загружает style.css - PullRequest
0 голосов
/ 31 декабря 2018

Я не могу получить файл functions.php моей темы WordPress, загрузить мою таблицу стилей.

Файлы, которые я создал в каталоге темы моей темы WordPress, соответствуют их содержанию.;

style.css

/*
Theme Name: Theme Study
Author: A
Version: 1.0.0
*/

body {
    color: #4d5a6c !important;
}

header.php

<!DOCTYPE html>
<html lang="en">
<head>
    <?php wp_head(); ?>
</head>
<body>
    <h2>This is the header</h2>

index.php

<?php get_header();

while ( have_posts() ) {
    the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content();
}
get_footer(); ?>

footer.php

    <h4>This is the footer</h4>
<?php wp_footer(); ?>
</body>
</html>

functions.php

<?php 
function style_files() {
    wp_enqueue_style( 'style_main', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'style_files' );

С другой стороны, я понял, что когда я ссылаюсь непосредственно на таблицу стилей внутри файла header.php file as below, it works as expected, but my aim is to achieve that in the functions.php` моей темы WordPress.

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_uri(); ?>" />
    <?php wp_head(); ?>
</head>
<body>

<h2>This is the header</h2>

Почему мои style.ccs не загружаются из файла functions.php?

Ответы [ 4 ]

0 голосов
/ 31 декабря 2018

Что, если вы попытаетесь поместить это в свой файл function.php:

<?php 
function style_files(){
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css');
}

add_action('wp_enqueue_scripts', 'style_files');

Тот, у которого идентификатор стиля назван: parent-style , как правило, для родительской темы (если вывы разрабатываете дочернюю тему или просто отдельную тему), вторая - если вы разрабатываете дочернюю тему и должны загрузить style.css в вашу дочернюю тему.

0 голосов
/ 31 декабря 2018

Используйте этот код, я думаю, он отлично подойдет для вас.

function theme_styles(){ 
// Load all of the styles that need to appear on all pages
wp_enqueue_style( 'main', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'custom', get_template_directory_uri() . '/css/custom.css' ); 
}
 add_action('wp_enqueue_scripts', 'theme_styles');
0 голосов
/ 31 декабря 2018

Используйте get_template_directory_uri() вместо get_stylesheet_uri(), как в вашем functions.php файле.

Ниже приведен фрагмент, иллюстрирующий, как он должен читаться;

function style_files() {
    wp_enqueue_style(
        'style_main',
        get_template_directory_uri() . '/style.css',
        array(),
        false,
        'all'
    );
}
add_action( 'wp_enqueue_scripts', 'style_files' );

Это безопасный способчтобы добавить / поставить в очередь файл таблицы стилей в вашей теме WordPress.

Выше приведен следующий формат:

wp_enqueue_style( $handle, $src, $deps, $ver, $media );

где

$handle
(string) (Required) Name of the stylesheet. Should be unique.

$src
(string) (Optional) Full URL of the stylesheet,
or path of the stylesheet relative to the WordPress root directory.

Default value: ''

$deps
(array) (Optional) An array of registered stylesheet handles this stylesheet depends on.

Default value: array()

$ver
(string|bool|null) (Optional) String specifying stylesheet version number, if it has one,
which is added to the URL as a query string for cache busting purposes. If version is set to
false, a version number is automatically added equal to current installed WordPress version.
If set to null, no version is added.

Default value: false

$media
(string) (Optional) The media for which this stylesheet has been defined. 
Accepts media types like 'all', 'print' and 'screen', or media queries like
'(orientation: portrait)' and '(max-width: 640px)'.

Default value: 'all'

Более подробная информация доступна здесь.

0 голосов
/ 31 декабря 2018

замените эту функцию вашей функцией wp_enqueue_style.

wp_enqueue_style( 'style', get_stylesheet_uri() );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...