2

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).

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?

1
  • Hi..When I said I wanted to enqueue a couple of scripts only to the archives for default post type, I actually meant that I only wanted it load only on default post type categories. But seems like I overly complicated the question which led to the (is_home()) possibility, which was not my case. I apologize. (is_category()) was a simple and so obvious solution for me. I knew I was missing something obvious. God, I feel so stupid. ! Salute to all the great folks for help anyways.
    – gurung
    Commented Sep 14, 2013 at 9:43

3 Answers 3

6

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');
 }
3
  • Yes, (is_category()) did the trick. Appreciate the help.
    – gurung
    Commented Sep 14, 2013 at 9:45
  • 1
    Doesn't this miss is_tag() etc features of the is_archive()? I find it perplexing I can't use is_post_type_archive( "post" ) to differentiate between blog posts and other post types.
    – lkraav
    Commented Jun 10, 2014 at 10:04
  • The standard post archive is the blog home page, so I think is_home() is the conditional you should use for is_post_type_archive( "post" ) but if you are in a tag, category, date or author archive you will need to add the proper conditionals.
    – cybmeta
    Commented Jun 10, 2014 at 10:18
1

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've deleted my answer. Always thinked is_home() works if posts page is setted. Upvoted for teach me this.
    – gmazzap
    Commented Sep 14, 2013 at 9:21
  • @G.M. yeah, the naming and logic around is_home()/is_front_page() is unholy mess. The modern meaning for is_home() is archive of post post type, but it's neither called that, neither archive. sigh
    – Rarst
    Commented Sep 14, 2013 at 9:29
  • What confused me is the fact that activating pretty permalinks there is no way to have a generic archive of standard post type without setting the posts page. Sure home_url() works... until I set a front static page.
    – gmazzap
    Commented Sep 14, 2013 at 9:33
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.