0

I'd like to load CSS but only on certain pages.

This code normally works great but I cant figure out the 'Conditional Tag' what will only load these pages.

This is the function I place in my functions.php file:

//Below loads JS ONLY for the main directory pages
function directory_scripts () {
        if ( *** NOT SURE *** ):
            wp_enqueue_style(
            'addevent js',
            get_stylesheet_directory_uri() . '/js/addevent.js',
            array()
        );
        endif;
 }

// add_action( 'wp_enqueue_scripts', 'directory_scripts' );

What I do normally is look in the <body> tag to see what I can reference but it just doesn't want to work...

Here is my <body> info:

<body class="conference_2018-template-default single single-conference_2018 postid-3025 wp-custom-logo oceanwp-theme fullscreen-mobile no-header-border default-breakpoint has-sidebar content-right-sidebar has-topbar page-header-disabled has-breadcrumbs elementor-default ">

Is it because I am using PODS Custom Post WordPress Framework?

My URL structure is like this:

http:// wwebsite.com / directory / page-here

^ I am trying to place the JS in the above 'page-here' pages...

Thanks for all help

  • If the page slug is page-here, you can use is_page( 'page-here' ) – Sally CJ Sep 8 '18 at 1:03
  • Hi again! That would only work once for one page - problem is that I have hundreds of pages (of course with different URL's/ slugs) so I am trying to discover a 'directory' level or other that I can associate with that function. – henry Sep 8 '18 at 1:06
0

Shouldn't is_singular() work:

Returns true if the post_type is "foo".

if ( is_singular( 'foo' ) ) {
  //code
}

Another option would be get_post_type():

Retrieves the post type of the current post or of a given post.

if ( 'foo' == get_post_type() ) {
 //code
}


In the end it is all a matter of how you structured your site, which is not nearly clear enough. So you might be better off to distinguish via category, taxonomy related conditional tags. E.g. for categories, maybe like this exmaple from the codex:

if ( is_category( 5 ) || cat_is_ancestor_of( 5, get_query_var( 'cat' ) ) ) {
  //code
}

Returns true when the category of posts being displayed is either term_id 5, or an ancestor of term_id 5 (subcategory, sub-subcategory...).


If you're actually trying to target sub-pages of a specific parent page, get_ancestors() might be an option. Exemplary:

if ( in_array( 123, get_ancestors( get_the_ID(), 'custom-post-type' ) ) {
  //code
}


This should get you a headstart, and give Codex: Conditional Tags another good read. By the way, I'm answering on the assumption that PODS uses the WordPress API to achieve its functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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