【问题标题】:style.css not found in Wordpress在 Wordpress 中找不到 style.css
【发布时间】:2019-11-11 08:16:46
【问题描述】:

我正在开发一个 wp-bootstrap-starter-child 主题的项目,并且我已经安装了 aceIDE 插件。我不知道 PHP 代码是如何工作的,问题是我的页面无法链接到我的 style.css。

它适用于另一个项目,但是当我尝试在我的项目中实现此代码时却没有。

这是我的页面: header.php:

<?php
/**
 * The header for our theme
 *
 * This is the template that displays all of the <head> section and everything up until <div id="content">
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package WP_Bootstrap_Starter
 */

?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="profile" href="http://gmpg.org/xfn/11">
<?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>
<div id="page" class="site">
    <a class="skip-link screen-reader-text" href="#content"><?php esc_html_e( 'Skip to content', 'wp-bootstrap-starter' ); ?></a>
    <?php if(!is_page_template( 'blank-page.php' ) && !is_page_template( 'blank-page-with-container.php' )): ?>
    <header id="masthead" class="site-header navbar-static-top <?php echo wp_bootstrap_starter_bg_class(); ?>" role="banner">
        <div class="container">
            <nav class="navbar navbar-expand-xl p-0">
                <div class="navbar-brand">
                    <?php if ( get_theme_mod( 'wp_bootstrap_starter_logo' ) ): ?>
                        <a href="<?php echo esc_url( home_url( '/' )); ?>">
                            <img src="<?php echo esc_url(get_theme_mod( 'wp_bootstrap_starter_logo' )); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>">
                        </a>
                    <?php else : ?>
                        <a class="site-title" href="<?php echo esc_url( home_url( '/' )); ?>"><?php esc_url(bloginfo('name')); ?></a>
                    <?php endif; ?>

                </div>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#main-nav" aria-controls="" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <?php
                wp_nav_menu(array(
                'theme_location'    => 'primary',
                'container'       => 'div',
                'container_id'    => 'main-nav',
                'container_class' => 'collapse navbar-collapse justify-content-end',
                'menu_id'         => false,
                'menu_class'      => 'navbar-nav',
                'depth'           => 3,
                'fallback_cb'     => 'wp_bootstrap_navwalker::fallback',
                'walker'          => new wp_bootstrap_navwalker()
                ));
                ?>

            </nav>
        </div>
    </header><!-- #masthead -->
    <?php if(is_front_page() && !get_theme_mod( 'header_banner_visibility' )): ?>
        <div id="page-sub-header" <?php if(has_header_image()) { ?>style="background-image: url('<?php header_image(); ?>');" <?php } ?>>
            <div class="container">
                <h1>
                    <?php
                    if(get_theme_mod( 'header_banner_title_setting' )){
                        echo get_theme_mod( 'header_banner_title_setting' );
                    }else{
                        echo 'WordPress + Bootstrap';
                    }
                    ?>
                </h1>
                <p>
                    <?php
                    if(get_theme_mod( 'header_banner_tagline_setting' )){
                        echo get_theme_mod( 'header_banner_tagline_setting' );
                }else{
                        echo esc_html__('To customize the contents of this header banner and other elements of your site, go to Dashboard > Appearance > Customize','wp-bootstrap-starter');
                    }
                    ?>
                </p>
                <a href="#content" class="page-scroller"><i class="fa fa-fw fa-angle-down"></i></a>
            </div>
        </div>
    <?php endif; ?>
    <div id="content" class="site-content">
        <div class="container">
            <div class="row">
                <?php endif; ?>

functions.php:

<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:

if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
    function chld_thm_cfg_locale_css( $uri ){
        if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
            $uri = get_template_directory_uri() . '/rtl.css';
        return $uri;
    }
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );

if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
    function chld_thm_cfg_parent_css() {
        wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( 'wp-bootstrap-starter-bootstrap-css','wp-bootstrap-starter-fontawesome-cdn' ) );
    }
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );

// END ENQUEUE PARENT ACTION

用于我的页面模板的hotel.php:

<?php
/**
* Template Name: Hotel
 */

get_header(); ?>

</div>
</div>

<section id="primary" class="content-area col-sm-12">
<main id="main" class="site-main" role="main">

<?php
while ( have_posts() ) : the_post();

get_template_part( 'template-parts/content', 'page' );

// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;

endwhile; // End of the loop.
?>

</main><!-- #main -->
</section><!-- #primary -->

<?php
get_footer();
?>

我不知道我错过了什么,因为我不太了解 PHP,所以我所做的只是从另一个项目中复制代码并对其进行重构很少 PS:对不起我的英语

【问题讨论】:

  • 如果尝试无条件实现样式,就像这里developer.wordpress.org/themes/basics/including-css-javascript/…_css后缀为wp_enqueue_style( 'chld_thm_cfg_parent',?
  • 还要确保依赖项 ('wp-bootstrap-starter-bootstrap-css','wp-bootstrap-starter-fontawesome-cdn') 能够加载。如果缺少一个,您的 style.css 将不会被加载。
  • 您能说得更具体些吗?我对php编码真的不太了解,也不知道该怎么做。例如,如何检查这些依赖项是否能够加载??

标签: php wordpress


【解决方案1】:

由于您使用子主题,请尝试在您的 functions.php 中使用此函数“get_stylesheet_directory_uri()”代替“get_template_directory_uri()”

【讨论】:

    【解决方案2】:

    请将此代码放入您的function.php文件中,并根据您的文件名和路径更改名称和路径。

    function add_theme_scripts() {
    // Css call here
    wp_enqueue_style('theme-css', get_template_directory_uri() . '/css/theme.css', false, false, 'all');
    
    // Javascript call here
    wp_enqueue_script('theme-js', get_template_directory_uri() . '/js/custom.js', false, false, true);
    }
    add_action('wp_enqueue_scripts', 'add_theme_scripts');
    

    【讨论】:

    • 仍然不行 :( 我加了这句话-> wp_enqueue_style('chld_thm_cfg_parent', get_template_directory_uri() . 'style.css', false, false, 'all'); 我不确定 chld_thm_cfg_parent虽然......但路径是正确的
    • 请将样式名称 chld_thm_cfg_parent 更改为 XYZ 并检查查看页面源哪个 URL 用于该样式。
    猜你喜欢
    • 1970-01-01
    • 2014-01-31
    • 2021-12-02
    • 2017-03-03
    • 2012-06-05
    • 2015-05-16
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多