condition for only if is archive for default post type

In order to optimize my site for a bit more loading speed, I wanted to enqueue a couple of scripts only to the archives for default post type. A rough example is given below.

function my_theme_script_enqueues() {

        if (!is_admin()) {

            if ( is_archive()) {
                wp_register_script('isotope', get_template_directory_uri() . '/js/jquery.isotope.min.js', 'jquery');
                wp_enqueue_script('isotope');
            }
        }
    }

add_action (blah..blah..blah..blah)

I assumed something like (is_archive(‘post’)) would work, but it still loads on cpt-archive and tag archives and such ( does not load on pages though).

Read More

I cant use (is_archive()) as it again loads it to every other archive types eg. cpt-archives, tags etc which is pointless to me.

If I use if (!is_post_type_archive('cpt')) {...} then it obviously goes wild and loads it everywhere else, not scalable.

I know how to use if( is_post_type_archive('post')) {...}, but it is not giving me any result, which it should i guess as ‘post’ is a post-type right? When I search for a solution here I get mostly threads talking about custom post type archive. Please let me know what my alternative is?

Related posts

3 comments

  1. is_archive() doesn’t accept any parameter and return true for every archive page: Categories, tag, date, author, …. I think what you need is to check if you are in a category page (in a archive of the category taxonomy) or in the blog home:

    if ( is_category() || is_home() ) {
        wp_register_script('isotope', get_template_directory_uri() . '/js/jquery.isotope.min.js', 'jquery');
        wp_enqueue_script('isotope');
     }
    
  2. You are probably looking for is_home().

    It’s inconsistent for historical reason. Concept of post type archives hadn’t appeared until after there were actually custom post types to have archives for.

    The basic index list of blog posts is is_home() and somewhat confusingly it’s not even is_archive().

  3. I ended up here via Google looking for a solution for all archives using the default post post type. Here’s how to accomplish that:

    Per @Rarst’s answer, is_archive() doesn’t work because it misses anything that fulfills the is_home() condition. And per the question itself, is_archive() also fails in this scenario because it returns true for CPT archives.

    This should work:

    if(get_post_type() == 'post' && !is_single()) {
        ...
    }
    

    get_post_type will return post for the “Posts Page” or any other archives or singular endpoints that are querying the post post type, and then !is_single() will skip the singular endpoints.

Comments are closed.