wp add inline style in loop

If grid_init() runs inside the Loop, then it executes after the page head and after the associated hooks– wp_head, wp_enqueue_scripts, etc. PHP executes line by line in sequence, so if you miss a hook you just missed it. You can’t go backwards. The script/style enqueueing subsystem does have to ability to put things int he footer though, and in this case that is the only option. I am sure that is what you are seeing.

The markup you are getting might work, but it is also invalid. Styles have to be in the head.

The simplest solution, and probably the best, is to echo inline style= tags in your markup.

If that solution doesn’t work then you will need to:

  1. Loop through the Loop in the head of the document– probably in the
    wp_enqueue_styles hook
  2. generate the markup
  3. reset the Loop
  4. then let the page load

There is obviously additional processing time involved in that, but unless you have a lot of posts per page, you probably won’t notice.