我的WordPress自定义主题不会获得CSS样式

时间:2016-12-24 06:11:10

标签: php css wordpress wordpress-theming custom-wordpress-pages

我正在学习如何从头开始制作WordPress主题&我现在面临着一个糟糕的问题。问题是我添加的CSS样式不适用于我主题的菜单导航。

这是index.php文件:

    <?php 
get_header();

if (have_posts()):
    while (have_posts()) : the_post(); ?>

    <article class="post">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <h2><?php the_content(); ?></h2>
    </article>

    <?php endwhile;

    else:
        echo '<p>No content found!</p>';

    endif;

get_footer();
?>

这是header.php文件:

    <!DOCTYPE html>
<html <?php language_attributes(); ?>>
    <head>
        <meta charset="<?php bloginfo('charset'); ?>">
        <meta name="viewport" content="width=device-width">
        <title><?php bloginfo('name'); ?></title>
        <?php wp_head(); ?>
    </head>

<body <?php body_class(); ?>>

    <div class="container">
        <header class="site-header">
            <h1><a href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a></h1>
            <h5><?php bloginfo('description'); ?></h5>

            <nav class="site-nav">
                <?php 
                $args = array(
                    'theme_location' => 'primary'
                );
                ?>
                <?php wp_nav_menu($args); ?>
            </nav>
        </header>

正如您所看到的,我将css类 site-nav 添加到 标记中,然后我将其编码为css样式:

.site-nav ul{
    margin:0;
    padding:0;
}
.site-nav ul:before, .site-nav ul:after{content: "";display:table;}
.site-nav ul:after{clear:both;}
.site-nav ul{*zoom:1;}
.site-nav ul li{
    list-style:none;
    float:left;
}

但每当我运行主题时,我都会看到这个屏幕:

enter image description here

但是请注意,我已经添加了functions.php添加CSS样式,并且我已经添加了其他CSS样式,例如body或者等等,它可以工作,但我不知道为什么菜单导航不会改变! !

这是functions.php

<?php 
function learningWordpress_resources(){
    wp_enqueue_style('style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts','learningWordpress_resources');
register_nav_menus(array(
    'primary' => __('Primary Menu'),
    'footer' => __('Footer Menu'),
));
?>

这是完整的CSS code

1 个答案:

答案 0 :(得分:1)

你必须提供课程

 <nav class="site-nav">
 <?php 
      $args = array(
      'theme_location' => 'primary',
       'menu_class'     => 'site_nav',
     );
   ?>
  <?php wp_nav_menu($args); ?>

转到检查这样的Css

#header .site-nav{} // container class
#header .site-nav ul {} // container class first unordered list
#header .site-nav ul ul {} //unordered list within an unordered list
#header .site-nav li {} // each navigation item
#header .site-nav li a {} // each navigation item anchor
#header .site-nav li ul {} // unordered list if there is drop down items
#header .site-nav li li {} // each drop down navigation item
#header .site-nav li li a {} // each drap down navigation item anchor

functions.php更改

add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
  register_nav_menus(array(
    'primary' => __('Primary Menu'),
    'footer' => __('Footer Menu'),
    ));
}
   add_theme_support('menus');

参见: http://www.wpbeginner.com/wp-themes/how-to-style-wordpress-navigation-menus/