Wordpress loads all assets and links with an absolute URL including the domain name (e.g. <img src="https://example.com/cat.png">).
How to NOT include the domain name?
I tried editing the WP_CONTENT_URL and updating WP_SITEURL/WP_HOME with no success.
Is there a simple way of doing that?
You can do that by simply WordPress theme URL function get_template_directory_uri(). See the below link.
https://developer.wordpress.org/reference/functions/get_template_directory_uri/
It will give a full path to the WordPress theme. If your image path is like this
\wp-content\themes\your-theme\assets\images\image.jpg
then you can get that image path like below.
<img src="<?php echo get_template_directory_uri() . '/assets/images/image.jpg'; ?>">
Related
I am trying to change the current path obtained bywp_get_attachment_url(get_post_thumbnail_id)
Now I get the uri as http://localhost/velocity/wordpress/wp-content/themes/velocity/images/pic01.jpg.
but I want to change the uri as http://newhost/velocity/wordpress/wp-content/themes/velocity/images/pic01.jpg
any idea?
The image you're referring to isn't an attachment image...it's a theme asset.
If you're properly including this in your template, changing hosts won't be a problem and the URL will automatically update. The following is an example of how you could be including the src in your template, using get_stylesheet_directory_uri():
<img src="<?php echo get_stylesheet_directory_uri() . '/images/pic01.jpg'; ?>">
Hello I want to insert my logo to my wordpress site
<img src=”/wordpress/wp-content/themes/test/images/logo.png”>
that is my FTP Path to the image but it's still does not work. When I refresh my site a image icon appears but it does not show my logo... Why? It seem that the PATH don't work right? Any suggestions what could be wrong or is there any special WP Query I need to use? Seems like it? Can't find tho..
Thanks
You shouldn't be using an absolute path to an image file. This would be useful if you wanted to get the files attributes in PHP but not when you want to show the image on the page.
There are two functions you should familiarise yourself with:
get_template_directory() - This will get the absolute path to the theme directory.
get_template_directory_uri() - This will get the theme directory URI.
Correct way to link to the image file:
<img src="<?php echo get_template_directory_uri(); ?>/images/logo.png">
Don't forget to add an alt attribute.
Use below function for site URL
site_url();
replace this with you img code
<img src="<?php echo site_url("/wp-content/themes/test/images/logo.png"); ?>">
use something like this
<img src="<?php bloginfo('template_url'); ?>/images/logo.png" />
I made an html / css mockup of a website and wanted to integrate that static website into wordpress. However, the images are not appearing, and I get the path of the image as the url localhost/wordpress/logo.png.
I'm developing it on localhost so the theme in localhost/wordpress/wp-content/themes/mytheme
and the images on localhost/wordpress/wp-content/themes/mytheme/images/.
I've heard that there's some php code that must be used in each image. Can anybody please help me out?
For what you need you have to use something like this:
<?php echo get_template_directory_uri() . '/images/logo.png; ?>
;)
i have some images loading in the header (header.php) from the location "../images/"
code is
<div class="header">
<img src="../images/headerimg.gif" alt=""/>
</div>
while the pages and custom post type load the image correctly from the location ..
the taxonomy.php doesn't load the image at all instead shows the cross sign which image is there but not loaded
plz help
In wordpress, you should never use such relative paths.
You should use one of the Built-in path functions , like get_template_directory_uri()
<?php echo get_template_directory_uri(); ?>/images/headerimg.gif">
or in the event you want to be included or ovverriden by child theme use get_stylesheet_directory_uri()
All this of course assumes that the images are in a sub-folder called images under the main theme folder. ( which is how it is supposed to be )
In case that the image is an Uploaded image , it should be referenced by the ID or the URL that is supplied from the upload . Even in the case you do not want to do so for some ( probably wrong ) reason , you should then also use wp functions like for example wp_upload_dir()
I have a custom WordPress theme installed and my images are referenced as such:
index.php:
<img src="images/newlogo.png" height="110px" width="400px"/>
My images folder is relative to my index.php from which I have included the above code.
My images are not showing when I render the webpage in a browser, it is showing the default image not found thumb instead.
Image filepath:
wp-content/themes/my-theme/images/newlogo.png
Homepage (index.php) filepath:
wp-content/themes/my-theme/index.php
Your index.php is a template that will be loaded by WordPress's main entry point (at /index.php) and the code behind it; as such its URL won't be ...wp-content/themes/my-theme/index.php. You should use the WordPress method get_stylesheet_directory_uri() to find your theme's directory from any of your theme template files.
For example:
<img src="<?php echo get_stylesheet_directory_uri() ?>/images/newlogo.png" height="110px" width="400px"/>
Basically, any relative paths to images from a web page are considered as relative to that web page's URL. However, with WordPress templates, you can't know for sure what the actual URL will be -- your index.php template might (probably will) be used for many different pages on your site, at many different URLs. It could be used at the top level for a page at /about, and also for an entry at /blog/2014/01/05/typical-blog-post, for example.
WordPress handles both building the URLs and loading your template file into them appropriately. But this means that you can't depend on the URL of the template you're writing being a constant. Instead, WordPress provides a series of functions, like get_stylesheet_directory_uri(), to let you grab and output the correct URI to your stylesheet files as necessary.