wordpress 3.5 自定义帖子类型,自定义元框,有条件地加载脚本

标签 wordpress wordpress-theming

我有这个奇怪的小问题 ;)。我只想在加载管理员的自定义帖子类型编辑器(添加新的自定义帖子类型、编辑自定义 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 不可用。

最佳答案

再搜索一点之后...

//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') );
}

附加或,并且只使用一个已知变量 - postID,它通过 $_GET 发送并使用 get_post_type 函数来完成这项工作。

关于wordpress 3.5 自定义帖子类型,自定义元框,有条件地加载脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14851615/

相关文章:

javascript - 应该很简单(但事实并非如此): Javascript snippet inside PHP

php - 如何使用 Zend Layout 作为 Wordpress 主题?

php - Wordpress - 类别和子类别的嵌套列表

wordpress - 更改WordPress的登录标签, "Username"

php - 更新后增加表单

wordpress - 高级自定义字段和重力形式

php - 在 Woocommerce 中为特定国家/地区自定义计算运输方式成本

php - 如何在wordpress主题中包含styles.css?

php - 如何摆脱 WordPress functions.php 中的 "SiteLock-PHP-FILEHACKER-of.UNOFFICIAL"

wordpress - 木材和 WPML 字符串翻译