Wednesday, 15 April 2015

Adding path to image in jQuery code in Wordpress -


I am adding a jQuery script to add a full screen image background to a page via backstrech ().

The problem I am facing is correcting the path of images, because I am using Wordpress.

This is what I have so far received:

    

I started url at & lt ?? Php bloginfo ('template_directory')? & Gt; I tried to use , but I believe it does not work in jQuery. How can I go about using the URL linking the image in this case?

Thank you.

Jason, I believe that you have a very classic challenge with WordPress and Javascript properties Are running. You need a PHP variable (or some type of PHP processing) for your Javascript, and of course, JavaScript can not parse PHP!

Some comments suggest inspection of the path (this has just happened to get your directory) but I am not a fan of that approach because it believes that your theme path is always the same Will happen. (And if you can change the name of your theme folder, change the theme, or just change your folder structure, then this can change.)

I usually use & lt; Head & gt; This is a very simple JSON object of all my subjects, to name-space (to keep the global scope clean).

  & lt; Script & gt; // Bootstrap Page Level Code for var YOURSITENAME = {"isFrontPage": & lt ;? = (Is_front_page ()? 'True': 'wrong'); ? & Gt ;, "iPad": & lt ;? = (Is_page ()? 'True': 'wrong'); ? & Gt ;, "PostName": '& lt ;? = Get_post_name (); ? & Gt; ',' TemplateURI ':' & lt;? = Get_template_directory_uri ();? & Gt; '}}; & lt; / script & gt;   

last item entitled "TemplateRie" See in your JavaScript Now you can access this value at any time by using the named JSON object:

or in your specific example: var imagePath = YOURSITENAME.templateURI + "/library/images/image.jpg"; JQuery (document) .ready (function ($) {$ ("Primary-Container"). Backstretch (Image Path );});

Of course, YOURSITENAME Change in some more meaningful namespace for.

Have fun!

No comments:

Post a Comment