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+')');
  • Are you sure that tiles contains a DOM element?
    – Naftali
    Aug 27, 2012 at 14:30

1 Answer 1


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
  • 1
    Or even (function($){})(jQuery); would work ^_^
    – Naftali
    Aug 27, 2012 at 14:33

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.