Title: Conditional ajax loading
Last modified: August 20, 2016

---

# Conditional ajax loading

 *  [sagive](https://wordpress.org/support/users/sagive/)
 * (@sagive)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/conditional-ajax-loading/)
 * i am trying to load some-ajax.php only when specific category
    is being viewed…
   but, no matter what i try or what action / hook i try to connect the “if($catid
   = ‘111’) {” condition the ajax stops working…
 * The condition is being met but i guess i got things mixed up
    with the order 
   of load..?
 * **in my functions.php i am testing for the category**
 *     ```
       global $post;
       $category = get_the_category($post->ID);
       $catID = $category[0]->term_id;
       if($catID == '111') {
        include(TEMPLATEPATH.'/ajaxLoops/tools/ajax-example.php');
       }
       ```
   
 * **Then inside “ajax-example.php” i have this code (just example):**
 *     ```
       wp_enqueue_script( 'ajax-site-screenshot', get_stylesheet_directory_uri().'/ajaxLoops/tools/ajax-get_site_screenshot.js', array('jquery'), 1.0 ); // jQuery will be included automatically
       wp_localize_script( 'ajax-site-screenshot', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl
   
       add_action( 'wp_ajax_action_site_screenshot', 'ajax_site_screenshot' ); // ajax for logged in users
       add_action( 'wp_ajax_nopriv_action_site_screenshot', 'ajax_site_screenshot' ); // ajax for logged in users
       function ajax_site_screenshot() {
       	$w150 = '
       	<div class="fullb">
       	<img src="http://s.wordpress.com/mshots/v1/' . urlencode($site_url) . '?w=150"/>
       	</div>';
   
       	echo '<div id="w150">'.$w150.'</div>';
   
       	die(); // stop executing script
       }
       ```
   
 * Could anyone help / guide / refrence me? i search for a similer
    problem or question
   but could not find any data on this.
 * My theme has 20-30 ajax files and i dont want to load each and
    every one of 
   them in every page when some are only used in specific category…
 * i would love for an example since i am a loss..
 * Thanks.
    Sagive

The topic ‘Conditional ajax loading’ is closed to new replies.

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)
 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [load](https://wordpress.org/support/topic-tag/load/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [sagive](https://wordpress.org/support/users/sagive/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/conditional-ajax-loading/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
