WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bit of javascript that will dynamically add a static post to a loop. The script uses the jQuery 'after' method and on a static page it works beautifully but when inside the loop I don't get any results.

Can anyone see what i'm doing wrong here? Does WP not allow DOM insertion within a loop?

function newTile(){
  var adTile = "<div class='new-box' id='article-tile'><h1> this is a test</h1>    </div>";
  var adLoc = 5;
  var tiles = $('#article-tile:nth-child('+adLoc+')');
share|improve this question
Are you sure that tiles contains a DOM element? – Neal Aug 27 '12 at 14:30
up vote 1 down vote accepted

WordPress requires no-conflict for jQuery.

Change this:

var tiles = $('#article-tile:nth-child('+adLoc+')');

...to this:

var tiles = jQuery('#article-tile:nth-child('+adLoc+')');

Or else wrap your entire script accordingly:

jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function
share|improve this answer
Or even (function($){})(jQuery); would work ^_^ – Neal Aug 27 '12 at 14:33
That did it, thanks! – HjalmarCarlson Aug 27 '12 at 14:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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