Using Conditionals with WP Enqueue Script and Genesis Framework

In my last post, I wrote about using wp_enqueue_script with the Genesis Framework. Today, I wanted to discuss using conditionals with wp_enqueue_script.

WordPress has a slew of conditional tags built in that allow you to display content on specific pages, page templates, posts, etc. Using WP Enqueue Script with conditionals gives you the best control over the scripts that are loading on your site, by keeping all of your code in one location.

Below is an example of how to load the Isotope jQuery plugin onto a portfolio page. The code should be placed in your functions.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/** Load scripts on Portfolio page */
add_action('genesis_after_footer', 'child_load_portfolio');
function child_load_portfolio() {
    if(is_page('Portfolio')) { 
        wp_register_script( 'isotope', get_bloginfo('stylesheet_directory').'/lib/js/jquery.isotope.min.js' );
        wp_enqueue_script ( 'isotope' );
    }
}
 
/** Isotope vars */
add_action('genesis_after', 'child_script_portfolio');
function child_script_portfolio() {
    if(is_page('Portfolio')) { ?>
      <script type="text/javascript">
          var $container = $('#container');
          // initialize isotope
          $container.isotope({
              layoutMode: 'fitRows',
          });
 
          // filter items when filter link is clicked
          $('#filters a').click(function(){
              var selector = $(this).attr('data-filter');
              $container.isotope({ filter: selector });
              return false;
          });
 
      </script>
<?php } }
/** Load scripts on Portfolio page */
add_action('genesis_after_footer', 'child_load_portfolio');
function child_load_portfolio() {
    if(is_page('Portfolio')) { 
        wp_register_script( 'isotope', get_bloginfo('stylesheet_directory').'/lib/js/jquery.isotope.min.js' );
        wp_enqueue_script ( 'isotope' );
    }
}

/** Isotope vars */
add_action('genesis_after', 'child_script_portfolio');
function child_script_portfolio() {
    if(is_page('Portfolio')) { ?>
      <script type="text/javascript">
          var $container = $('#container');
          // initialize isotope
          $container.isotope({
              layoutMode: 'fitRows',
          });

          // filter items when filter link is clicked
          $('#filters a').click(function(){
              var selector = $(this).attr('data-filter');
              $container.isotope({ filter: selector });
              return false;
          });

      </script>
<?php } }

So lets go over it, line by line, to cover what this script is doing.

Line 2: Add our new function after the Genesis closes the footer tag.

Line 4: If the current page is titled Portfolio, execute the code inside the brackets. If not, move along.

Line 5: Register the Isotope script, as well as it’s location. The get_bloginfo('stylesheet_directory') section is especially important when dealing with Genesis, as it points to the child theme folder.

Line 6: Enqueue the script.

As far as registering and enqueuing the script, that’s all there is to it. However, Lines 11 through 29 deal with loading the variables for the Isotope script; so lets look at that, too.

Line 11: This part is especially important. The script variables have to be loaded after its dependency has been loaded (in this case Isotope).

Line 13: Again, if the current page is titled Portfolio, execute the code inside the brackets. If not, move along.

Lines 14-28 These lines are where you place your script variables.

If you found this post helpful, please share it. If you have any questions, I’m more than happy to lend a hand!

About Jon Bellah

I am a graduate of Louisiana State University, an out-of-place Saints fan and former college sports writer. I am the front-end developer for a small, boutique ad agency in Dallas, TX.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
BinHuang 5 pts

Hi Jon,

Could you guide me a bit how I can integrate Isotope with Filters for my portfolio category for Genesis child theme? I've been trying to do that for a while and I don't know how to wrap each item, and generate filters with portfolio categories. Thank you so much!

JonBellah 8 pts moderator

 BinHuang Bin, I'm going to make a separate post on this, but I went ahead and created a gist, so you wouldn't have to wait for me to write the article.

 

Add this to your functions.php - https://gist.github.com/JonBellah/5586321

 

And here is the HTML - https://gist.github.com/JonBellah/5586354

JonBellah 8 pts moderator

 BinHuang Also, in that gist where it says "$args = array( 'taxonomy' => 'skills' );", make sure that 'skills' is changed to whatever you named your custom post type taxonomy.

BinHuang 5 pts

 JonBellah  BinHuang Hi, I pasted my page_portfolio.php page, could you take a look at it cause apparently it's not really working... I put the coniditional enqueue script as well as the custom taxonomy query in the functions.php page. My taxonomy is called project_category, and the custom post type is called project. Thanks! Here's the link:

 

https://gist.github.com/anonymous/9b15cf27d4d5010fc829

talentedaamer 5 pts

thanks you for the script, it helped me to add jquery back to top to Genesis Child Theme

JonBellah 8 pts moderator

 talentedaamer No problem! I'm glad you found it useful. 

davewmerce 5 pts

You mention the jQuery Plugin, is this a plugin available for Wordpress or is it the one downloaded from Isotope website?

 

Regards

 

Dave

JonBellah 8 pts moderator

davewmerce

Dave,

 

jQuery plug-ins are extensions of the functionality of the overall jQuery library. They're not like the plug-ins that you're used to with WordPress, though. You include jQuery plug-ins in your site with the <script src="jquery.plug-in.js" type="text/javascript"> </script>.

 

Most high quality jQuery plug-ins come with step-by-step documentation on how to implement and customize them on your site. 

 

 

Jon