Change the Path of wordpress featured image - wordpress

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'; ?>">

Related

How to remove hardcoded domain name from WordPress links?

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'; ?>">

Showing Picture with Advanced Custom fields /fishpig extension

Using advanced custom fields with fishpig extension just shows URL or “Array” or “ID” of the picture in fronted.
How can the picture be shown.
When creating a field of Image type, you can choose the return type. If you are using the latest version of the FishPig Advanced Custom Fields add-on extension (1.4.0.1), you have 3 options for the return type:
Object
URL
Array
If you choose object, the image model will be returned. Once you have the image model you can get any of the different URLs for the image (each URL is for a different sized image). For a list of methods you can call to get different images, see this article.
If you choose URL, the URL to the originally uploaded image will be returned.
If you choose array, an array will be returned that contains the image ID, URL and object. (eg. $image['id'], $image['object'] and $image['url']). To convert the image ID to an object, use the following code:
<?php $image = Mage::getModel('wordpress/image')->load($image['id']) ?>
<?php if ($image->getId()): ?>
<img src="<?php echo $image->getAvailableImage() ?>" alt="" />
<?php endif; ?>
If you aren't already, I would recommend upgrading Magento WordPress Integration and Advanced Custom Fields to the latest versions.
when echoing out the URL, run this inside an img src.
i.e.
<img src="<?php echo the_field('image-url') ?>" />

Insert logo header.php wordpress

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" />

taxonomy.php doesn't display images from "../image/" folder

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()

how to assign image dynamically in wordpress post

I am developing one wordpress site. I have made one post in it. Now I want to assign image in that post. For this, I have uploaded one image in media and attach that image to my post.
but how can I assign that uploaded image in my post? do I need to give path manually? or I can give dynamic path so that even if I upload this site on server or change main folder name, path gets changed automatically..
any help will be appreciated..
thank you
You should consider using Post Thumbnails (also known as Featured Images) in your posts. To do this, all you need to do is add the following to your functions.php file:
add_theme_support('post-thumbnails');
This will add a control in your Post Editor to add an image by either Uploading it or setting the URL, effectively "attaching" your image to your post in the way your looking for. To display the image in your template:
<?php
if(have_posts()) : while(have_posts()) : the_post();
if(has_post_thumbnail())
echo '<div class="post_thumb">'.get_the_post_thumbnail().'</div>';
?>
<div class="post_content"><?php the_content(); ?></div>
<?php
endwhile;endif;
?>
As for changing folder names, paths, etc. you need to be careful with that approach. Remember that you're giving a path to an asset. The server doesn't know what YOU want, only what your code is requesting. If you expect to be changing paths to your assets around quite a bit, then you can always forgo Post Thumbnails in favor of clever naming conventions. Something like this:
<?php
if(have_posts()) : while(have_posts()) : the_post();
$imgPath = get_bloginfo('stylesheet_directory').'/images/featured_'.$post->post_name.'.jpg';
?>
<div class="post_thumb"><img src="<?php echo $imgPath; ?>" /></div>
<div class="post_content"><?php the_content(); ?></div>
<?php
endwhile;endif;
?>
This looks for an image in your Theme Directory's images folder that is named featured_{post_slug}.jpg
The benefits to this approach is that Wordpress will always know where your theme folder is, regardless of URL changes. As long as you have an images folder in your theme directory, Wordpress will know where to look.
The drawback is that this code specifically doesn't first check for the EXISTENCE of the image before displaying it, which could lead to broken images if they aren't named properly or don't exist at all. This approach also requires the use of one file extension
A last option for you is to consider using Custom Fields to define paths to images. The benefit is that this does not require you to actually upload images to your server. However, this approach is still the least dynamic out of all of your options, and will likely break if paths to assets are changed.
Use whichever tool you feel is best for the job. Hope this helps!

Resources