wp_enqueue_style将css放在页脚而不是标题中

时间:2014-03-20 09:34:26

标签: css wordpress action footer

我有以下代码片段用于在WordPress管理区域的设置页面上包含样式和java脚本。我有一个单独的类,我用它来启动我的管理页面。我删除了除了所需代码之外的所有内容,以使其更具可读性。

我遇到的问题是,使用下面的设置是设置页面上的样式表放在页面的底部而不是页面的头部。我可以通过使用其他动作挂钩在标题中获取它,但这会破坏目的。据我所知,我使用的设置与wp_enqueue_style命令描述的设置相同。

命令“wp_enqueue_style()现在可以在页面中间调用(在HTML正文中)有一个小提示。这将在页脚中加载样式。”如果这是真的,那意味着'admin_print_scripts- *'钩子在页面中间而不是在开始时被调用,这样就把css放在页脚中。

对此有任何想法。我做错了什么?

感谢您的时间。

这是在functions.php文件中调用单例类的方法

theme::instance( );

这是我用来创建管理页面的类的一部分

class theme {
  static public function instance( )
  {
    is_null( self::$instance ) AND self::$instance = new self;

    return self::$instance;
  }

  public function __construct()
  {
    add_action( 'admin_menu', array( $this, 'initMenu' ), 10, 0 );
    add_action( 'admin_init', array( $this, 'registerAssets' ), 10, 0 );
  }

  public function registerAssets( )
  {
    // Styles
    wp_register_style( 'cen', 'style.css', array( ), '1.0' );
    wp_register_style( 'cen-settings', 'settings.css', array( 'cen' ), '1.0' );

    // Scripts
    wp_register_script( 'cen', 'settings.js', array( 'jquery' ), '1.0', true );
  }

  public function initMenu( )
  {
    // Index page
    $index =add_menu_page( 'Cen', 'Cen', 'manage_options', 'cen-index', function (){ require_once( get_template_directory( ) . '/pages/index.php' ); }, get_template_directory_uri() . '/images/icons/logo_16.png', "110.00" );

    // Settings page
    $settings =add_submenu_page( 'cen-index', 'Cen - Settings', 'cen' ), 'Settings', 'manage_options', 'cen-settings', function (){ require_once( get_template_directory( ) . '/pages/settings.php' ); } );

    // Add action for assets on the settings page
    add_action( 'admin_print_scripts-' . $settings, array( $this, 'initSettingsPage' ));
  }

  public function initSettingsPage( )
  {
    // Styles used
    wp_enqueue_style( 'cen' );
    wp_enqueue_style( 'cen-settings' );

    // Scripts used
    wp_enqueue_script( 'jquery-ui-tabs' );
    wp_enqueue_script( 'cen' );
  }
}

1 个答案:

答案 0 :(得分:2)

您正在使用的操作挂钩admin_print_scripts用于添加内联脚本,因此强烈建议您使用admin_enqueue_scripts将管理员中的脚本/样式排入队列。 试试吧。希望它有效!

相关问题