主题将css注入wp-admin

时间:2015-05-04 作者:Steve

我的主题中的一些CSS正在wp admin中使用。

例如,“添加新页面”/wp-admin/post-new.php?post_type=page 是蓝色的,这来自我的主题,正如Chrome代码检查器所述。

enter image description here

这是我的主题的功能。php:

<?php

if ( ! isset( $content_width ) )
    $content_width = 940;

function register_my_menu() {
  register_nav_menu(\'primary\',__( \'Primary Menu\' ));
  register_nav_menu(\'footer\',__( \'Footer Menu\' ));
}

add_action( \'init\', \'register_my_menu\' );


function simple_rabbit_widgets_init() {
    register_sidebar( array(
        \'name\' => __( \'Main Sidebar\', \'simple-rabbit\' ),
        \'id\' => \'sidebar-0\',
        \'description\' => __( \'Appears on all pages\', \'simple-rabbit\' ), 
        \'before_widget\' => \'<div id="%1$s" class="widget %2$s">\',
        \'after_widget\' => \'</div>\',
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\',
    ) );
    register_sidebar( array(
        \'name\' => __( \'Footer Widget Area\', \'simple-rabbit\' ),
        \'id\' => \'sidebar-1\',
        \'description\' => __( \'Appears on all pages\', \'simple-rabbit\' ), 
        \'before_widget\' => \'<div id="%1$s" class="widget %2$s">\',
        \'after_widget\' => \'</div>\',
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\',
    ) );
}

add_action( \'widgets_init\', \'simple_rabbit_widgets_init\' );

wp_register_style( \'mainstyle\', get_stylesheet_uri());
wp_enqueue_style( \'mainstyle\', get_stylesheet_uri());

/* 
 * Loads the Options Panel
 *
 * If you\'re loading from a child theme use stylesheet_directory
 * instead of template_directory
 */

if ( !function_exists( \'optionsframework_init\' ) ) {
    define( \'OPTIONS_FRAMEWORK_DIRECTORY\', get_template_directory_uri() . \'/inc/\' );
    require_once dirname( __FILE__ ) . \'/inc/options-framework.php\';
}

/* 
 * This is an example of how to add custom scripts to the options panel.
 * This one shows/hides the an option when a checkbox is clicked.
 *
 * You can delete it if you not using that option
 */

add_action(\'optionsframework_custom_scripts\', \'optionsframework_custom_scripts\');



function twentytwelve_comment( $comment, $args, $depth ) {
    $GLOBALS[\'comment\'] = $comment;
    switch ( $comment->comment_type ) :
        case \'pingback\' :
        case \'trackback\' :
        // Display trackbacks differently than normal comments.
    ?>

<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
  <p>
    <?php _e( \'Pingback:\', \'twentytwelve\' ); ?>
    <?php comment_author_link(); ?>
    <?php edit_comment_link( __( \'(Edit)\', \'twentytwelve\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
  </p>
  <?php
    break;
    default :
    // Proceed with normal comments.
    global $post;
  ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  <article id="comment-<?php comment_ID(); ?>" class="comment">
    <header class="comment-meta comment-author vcard">
      <?php
        echo get_avatar( $comment, 44 );
        printf( \'<cite class="fn">%1$s %2$s</cite>\',
        get_comment_author_link(),
        // If current post author is also comment author, make it known visually.
            ( $comment->user_id === $post->post_author ) ? \'<span> \' . __( \'Post author\', \'twentytwelve\' ) . \'</span>\' : \'\');
                printf( \'<a href="%1$s"><time datetime="%2$s">%3$s</time></a>\',
                esc_url( get_comment_link( $comment->comment_ID ) ),
                get_comment_time( \'c\' ),
                /* translators: 1: date, 2: time */
                sprintf( __( \'%1$s at %2$s\', \'twentytwelve\' ), get_comment_date(), get_comment_time() )
                );
        ?>
    </header>
    <!-- .comment-meta -->

    <?php if ( \'0\' == $comment->comment_approved ) : ?>
    <p class="comment-awaiting-moderation">
      <?php _e( \'Your comment is awaiting moderation.\', \'twentytwelve\' ); ?>
    </p>
    <?php endif; ?>
    <section class="comment-content comment">
      <?php comment_text(); ?>
      <?php edit_comment_link( __( \'Edit\', \'twentytwelve\' ), \'<p class="edit-link">\', \'</p>\' ); ?>
    </section>
    <!-- .comment-content -->

    <div class="reply">
      <?php comment_reply_link( array_merge( $args, array( \'reply_text\' => __( \'Reply\', \'twentytwelve\' ), \'after\' => \' <span>&darr;</span>\', \'depth\' => $depth, \'max_depth\' => $args[\'max_depth\'] ) ) ); ?>
    </div>
    <!-- .reply --> 

  </article>
  <!-- #comment-## -->

  <?php
    break;
    endswitch; // end comment_type check
}

function optionsframework_custom_scripts() { ?>
  <script type="text/javascript">
jQuery(document).ready(function() {

    jQuery(\'#example_showhidden\').click(function() {
        jQuery(\'#section-example_text_hidden\').fadeToggle(400);
    });

    if (jQuery(\'#example_showhidden:checked\').val() !== undefined) {
        jQuery(\'#section-example_text_hidden\').show();
    }

});
</script>
  <?php
}

function simple_rabbit_scripts_styles() {

    if ( is_singular() && comments_open() && get_option( \'thread_comments\' ) )
        wp_enqueue_script( \'comment-reply\' );

}
add_action( \'wp_enqueue_scripts\', \'simple_rabbit_scripts_styles\' );

add_theme_support( \'automatic-feed-links\' );
你能帮我确定CSS是如何注入的吗?非常感谢。

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

问题是您将主题的CSS文件排在任何推荐操作之外。如果希望仅在前端添加,则应使用wp_enqueue_scripts 措施:

add_action( \'wp_enqueue_scripts\', function() {
    wp_register_style( \'mainstyle\', get_stylesheet_uri());
    wp_enqueue_style( \'mainstyle\', get_stylesheet_uri());
});

结束

相关推荐

将动态CSS生成到自定义文件中

这是关于在wordpress上运行代码的。给定的代码在wordpress外部正常运行,但当我将其放在主题函数文件中时,它并没有做任何事情。ob_start(); require(\'dynamic-css.php\'); $content = ob_get_contents(); ob_end_clean(); $f = fopen(\"custom.css\", \"w\"); fwrite($f, $content); fclose($f);