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

customization - enqueue script on custom post type archive page

programmeradmin0浏览0评论

How do I enqueue a script on the header if the user is on an archive page of a custom post type? The custom post type is called limitedrun and the page is called archive-limitedrun.php.

I tried this and got no response:

function opby_theme() {
wp_register_script('responsive-img', get_template_directory_uri() .'/js/responsive-img.min.js', array('jquery'));
wp_enqueue_script('responsive-img');
wp_enqueue_style( 'opby', get_stylesheet_uri() );
wp_register_style('googleFonts', '+Slab:400,100|Bitter');
wp_enqueue_style( 'googleFonts');
if (is_singular('limitedrun') ) {
wp_register_style('ltd-run-font', ':100,500');
wp_enqueue_style( 'ltd-run-font');
}
}
add_action( 'wp_enqueue_scripts', 'opby_theme' );  

How do I enqueue a script on the header if the user is on an archive page of a custom post type? The custom post type is called limitedrun and the page is called archive-limitedrun.php.

I tried this and got no response:

function opby_theme() {
wp_register_script('responsive-img', get_template_directory_uri() .'/js/responsive-img.min.js', array('jquery'));
wp_enqueue_script('responsive-img');
wp_enqueue_style( 'opby', get_stylesheet_uri() );
wp_register_style('googleFonts', 'https://fonts.googleapis.co/css?family=Roboto+Slab:400,100|Bitter');
wp_enqueue_style( 'googleFonts');
if (is_singular('limitedrun') ) {
wp_register_style('ltd-run-font', 'https://fonts.googleapis/css?family=Roboto:100,500');
wp_enqueue_style( 'ltd-run-font');
}
}
add_action( 'wp_enqueue_scripts', 'opby_theme' );  
Share Improve this question asked May 24, 2016 at 22:06 Gregory SchultzGregory Schultz 6236 silver badges31 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

You can save your time and server load by not using wp_register_script and wp_register_style when you don't need them definitely. wp_enqueue_style and wp_enqueue_script do the same job themselves when not involving excessive functions.

Here is easier and more readable code up to date with the accepted answer by @vancoder:

<?php
function opby_theme()
{
    wp_enqueue_script(
        'responsive-img',
         get_template_directory_uri() .'/js/responsive-img.min.js',
         array('jquery')
    );

    wp_enqueue_style(
        'opby',
        get_stylesheet_uri()
    );

    wp_enqueue_style(
        'googleFonts',
        'https://fonts.googleapis.co/css?family=Roboto+Slab:400,100|Bitter'
    );

    if ( is_post_type_archive('limitedrun') ) {
        wp_enqueue_style(
            'ltd-run-font',
            'https://fonts.googleapis/css?family=Roboto:100,500'
        );
    }
}
add_action( 'wp_enqueue_scripts', 'opby_theme' );

is_singular doesn't handle archives.

Try

is_post_type_archive( 'limitedrun' );

You can use the template_redirect action hook for exactly that.

This hook is the point where WordPress knows which templates the user is viewing and gets fired just before the template files are loaded.

Here is an example, where a stylesheet is only loaded for the post archive:

`

// Call the hook and register your function
add_action( 'template_redirect', 'register_styles_for_post_archive' );

// Call your function which enqueues the stylesheet only for the post archive
function register_styles_for_post_archive() {

// put in your post type as a parameter or leave empty for the actual post type
    if ( is_post_type_archive( $post_types ) ) {
        wp_enqueue_style('your-custom-stylesheet-name', 'your-css-file.css', false, 01, 'screen');

    }
}

`

发布评论

评论列表(0)

  1. 暂无评论