3

我有这个奇怪的小问题;)。我只想在加载管理员的自定义帖子类型编辑器(添加新的自定义帖子类型、编辑自定义 mpost 类型)时加载自定义脚本(css、js)。

自定义帖子类型以这种方式注册:

add_action( 'init', 'cs_product_register_post_types' );
function cs_product_register_post_types() {
    $product_args = array(
        'public' => true,
        'rewrite' => array(
            'slug' => 'products',
            'with_front' => false,
            'pages' => false
        ),
        'supports' => array(
            'title',
            'editor',
            'page-attributes'
        ),
        'labels' => array(
            'name' => 'Produkty',
            'singular_name' => 'Produkt',
            'add_new' => 'Dodaj nowy produkt',
            'add_new_item' => 'Dodaj nowy produkt',
            'edit_item' => 'Edytuj produkt',
            'new_item' => 'Nowy produkt',
            'view_item' => 'Wyswietl produkt',
            'search_items' => 'Wyszukaj produkt',
            'not_found' => 'Produktu nie znaleziono',
            'not_found_in_trash' => 'Brak usuniętych produktów'
        ),
        'menu_position' => 3,
    );
    register_post_type( 'products', $product_args );
}

对于那些有注册自定义元框的功能:

add_action( 'add_meta_boxes', 'cs_products_mb_create' );
function cs_products_mb_create() {
    //create a custom meta box
    add_meta_box( 'products-info', 'Ustawienia Produktu', 'cs_products_mb_function', 'products', 'normal', 'high' );
}

这仅适用于注册的自定义帖子类型(产品)。

现在唯一要做的就是加载自定义js。可以这样做:

add_action('admin_print_styles-post.php', 'cs_products_admin_styles');
add_action('admin_print_styles-post-new.php', 'cs_products_admin_styles');

function cs_products_admin_styles() {
    wp_enqueue_style( 'thickbox' );
    wp_enqueue_style ('theme', get_bloginfo('template_url') . '/css/admin.css', '', '1.0');
} 

但它适用于所有帖子,它不是最好的方法;)。

感谢您的任何想法。


编辑

在挖掘,挖掘和挖掘之后......最简单的方法之一:

//load scripts only when on products custom post type edit page
if ( ( isset($_GET['post_type']) && $_GET['post_type'] == 'products' ) || ( isset($post_type) && $post_type == 'products' ) ) {
    add_action('admin_enqueue_scripts', 'cs_admin_customposttype_scripts');
}

function cs_admin_customposttype_scripts(){ 
    wp_enqueue_style ('theme', get_bloginfo('template_url') . '/css/admin.css', '');    
    wp_enqueue_script( 'cs-image-upload', get_bloginfo('template_url').'/js/admin.js', array( 'jquery') );
}

但是这个解决方案的问题是,它仅在创建新的自定义帖子时才有效。编辑时,$_GET['post_type'] 或 $post_type 不可用。

4

1 回答 1

2

在搜索了一点之后......

//load scripts only when on products custom post type edit page
if ( ( isset($_GET['post_type']) && $_GET['post_type'] == 'products' ) || ( isset($post_type) && $post_type == 'products' ) || ( isset($_GET['post']) && get_post_type($_GET['post']) == 'products' ) ) {
    add_action('admin_enqueue_scripts', 'cs_admin_customposttype_scripts');
}

function cs_admin_customposttype_scripts(){ 
    wp_enqueue_style ('theme', get_bloginfo('template_url') . '/css/admin.css', '');    
    wp_enqueue_script( 'cs-image-upload', get_bloginfo('template_url').'/js/admin.js', array( 'jquery') );
}

附加或仅使用一个已知变量 - 使用 $_GET 发送并使用 get_post_type 函数的 postID 完成了这项工作。

于 2013-02-13T11:10:53.330 回答