2012-06-03 109 views
0

我的當前PHP代碼正在工作,並按我希望的樣子使用我的「主題選項」頁面(位於WP API外觀菜單下)。將CSS應用於「主題選項」頁面僅在Wordpress中

CSS樣式表也適用於WP儀表板中的每個其他菜單(例如影響「設置>常規選項」)頁面。我該如何才能將樣式表應用到我的「主題選項」頁面,而不是篡改其他任何東西?

我的樣式表被命名爲'theme-options.css',它位於一個名爲「include」> include/theme-options.css的文件夾中。下面的代碼被放置在「theme-options.php」頁面。

<?php 
// Add our CSS Styling 
add_action('admin_menu', 'admin_enqueue_scripts'); 
function admin_enqueue_scripts() { 
    wp_enqueue_style('theme-options', get_template_directory_uri() . '/include/theme-options.css'); 
    wp_enqueue_script('theme-options', get_template_directory_uri() . '/include/theme-options.js'); 
} 

回答

4

我分別從我的網頁(略高於該功能)的積木放置CSS & JS文件。代碼現在裏面我的頁面構建函數,我現在正在得到我以後的結果。

以前:

... 
// Add our CSS Styling 
function theme_admin_enqueue_scripts($hook_suffix) { 
    wp_enqueue_style('theme-options', get_template_directory_uri() . '/include/theme-options.css', false, '1.0'); 
    wp_enqueue_script('theme-options', get_template_directory_uri() . '/include/theme-options.js', array('jquery'), '1.0');  

// Build our Page 
function build_options_page() { 

ob_start(); ?> 
    <div class="wrap"> 
     <?php screen_icon();?> 

     <h2>Theme Options</h2> 

     <form method="post" action="options.php" enctype="multipart/form-data"> 

     ... 
     ... 

解決方案:

// Build our Page 
function build_options_page() { 

// Add our CSS Styling 
wp_enqueue_style('theme-options', get_template_directory_uri() . '/include/theme-options.css'); 
wp_enqueue_script('theme-options', get_template_directory_uri() . '/include/theme-options.js'); 

ob_start(); ?> 
    <div class="wrap"> 
     <?php screen_icon();?> 

     <h2>Theme Options</h2> 

     <form method="post" action="options.php" enctype="multipart/form-data"> 

     ... 
     ... 
+0

'is_page'不適合我,這種方法確實如此。 – gurung

1

你只能如果當前頁面是你的特殊的頁面由checking the page前添加CSS文件,如:

if (is_page('Theme Options')) { // check post_title, post_name or ID here 
    add_action('admin_menu', 'admin_enqueue_scripts'); 
} 

=== UPDATE ===

也許是BETT呃檢查的功能:

<?php 
// Add our CSS Styling 
add_action('admin_menu', 'admin_enqueue_scripts'); 
function admin_enqueue_scripts() { 
    if (is_page('Theme Options')) { // check post_title, post_name or ID here 
     wp_enqueue_style('theme-options', get_template_directory_uri() . '/include/theme-options.css'); 
    } 
    wp_enqueue_script('theme-options', get_template_directory_uri() . '/include/theme-options.js'); 
} 
+0

感謝您的快速響應,並從,我發現我的解決方案。 – user1752759

相關問題