最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Enqueue plugin for specific pages

programmeradmin5浏览0评论

I want to enqueue my plugin for certain pages. I've tried several things but nothing worked so far:

add_action( 'init', 'my_enqueue' );
function my_enqueue() {
    global $post;
    if( $post->ID == 380 || is_home() || is_front_page() || is_single(380) || is_page(380)) {
        wp_enqueue_script( 'lister_js',  plugins_url( '/js/lister.js', __FILE__ ), array('jquery'), filemtime( '/js/lister.js', __FILE__ ));
        wp_localize_script( 'lister_js', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    }
}

The post id is 380 according to the db. I even checked the url and it is ".../wp-admin/post.php?post=380&action=edit". So I'm pretty sure the id is correct.

I want to enqueue my plugin for certain pages. I've tried several things but nothing worked so far:

add_action( 'init', 'my_enqueue' );
function my_enqueue() {
    global $post;
    if( $post->ID == 380 || is_home() || is_front_page() || is_single(380) || is_page(380)) {
        wp_enqueue_script( 'lister_js',  plugins_url( '/js/lister.js', __FILE__ ), array('jquery'), filemtime( '/js/lister.js', __FILE__ ));
        wp_localize_script( 'lister_js', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    }
}

The post id is 380 according to the db. I even checked the url and it is ".../wp-admin/post.php?post=380&action=edit". So I'm pretty sure the id is correct.

Share Improve this question asked Jun 20, 2020 at 12:18 KitiaraKitiara 1114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It started to work once i change

add_action( 'init', 'my_enqueue' );

to

add_action( 'wp_enqueue_scripts', 'my_enqueue' );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论