0

I'm having problem how to get image url using jquery. I have custom.js inside my js folder and i want to get the image from img folder.. By the way I'm using wordpress. Maybe someone can help me out?.

var $old_img = '../<?php bloginfo('stylesheet_directory');?>'+'/img/qs_slogan_down.png'; 
1
  • Hi, could you indicate how this relates to jquery ? To get the url from an img tag using jquery you use $('img').attr('src') , making the jquery selector as specific as you need. Or maybe change the title to "get image url using php in wordpress" Commented Jul 12, 2013 at 0:21

1 Answer 1

2

So I am assuming that your "img" directory is in the root of your wordpress theme? So for instance if your theme name is "my custom theme" the path might look something like...

public_html/wp-content/themes/my custom theme/img/qs_slogan_down.png

If that is indeed the case, then I would just assign the variable to the correct path, and you would do that thusly: '<?php bloginfo('template_directory');?>/img/qs_slogan_down.png'.

Template directory refers to the your theme folder inside of wp-content.

That being said, you are still only storing a string in your variable, which you would want to assign to the src attribute of an image.

EDIT:

I think I understand your dilemma. You want a usable filepath, and if you are trying to get that in a function that gets triggered after the document load, then you aren't going to receive any information from the server because the information has already been served.

So you either need to use AJAX (probably a bit too complicated, but good to learn about), or you can do something like the following:

In your .php document, use the bloginfo('template_directory') function to call and store the data you need on the page (as some attribute of some element) during the document load, and then pass it to your function as a parameter, and assign your variable to the corresponding parameter so that you have the explicit file path ready to use.

Hope that helps! :-)

Sign up to request clarification or add additional context in comments.

3 Comments

nice suggestion out there..but for instance I'm going to change the site url.. So what I want is to make it dynamic. :))
Just getting to used to stackoverflow... I see that it omitted some of my answer. Sorry about that! Read again after "thusly:"
i used the template directory but it give and error that says Uncaught SyntaxError: Unexpected identifier

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.