I am very new in wordpress and I noticed that at my DB im saving values as 20 on a custom tag, to print it they use something like this:
print_custom_field('my_logo:to_image_src')
And basically, it prints the image URL.
Reading this I noticed it says accepts the ID of an image and returs the URL
I have on my meta_key and meta_value fields the following
meta_key = my_logo
meta_value = 20
Still, not sure how to make it work?
I bring the meta_key and meta_value value with a query. So, basically, I dont know how can I come from there to get the path (as I have it on a previous template).
Thanks
Try adding the following code where you want the images to appear:
<?php $image = wp_get_attachment_image_src(get_post_meta( get_the_ID(), 'my_logo' ), 'full'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_post_meta( get_the_ID(), 'my_logo' )) ?>" />
This will get the full-sized image for you using the ID that is returned from the my_logo key.
Related
I'm using Gutenberg default post editor to edit my WordPress posts.
I have tried serval approaches to display the post's gallery, but none of them worked.
<?php
var_dump($_GET['id']);
echo '<br/>';
echo '<br/>';
$gallery = get_post_gallery( $_GET['id'] /* The post ID from $_GET */, false );
// Loop through all the image and output them one by one.
foreach ( $gallery['src'] AS $src ) {
?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
<?php
}
?>
Debugging notices:
string(3) "404"
Notice
: Trying to access array offset on value of type bool in
/home/mialuzco/domains/mia-luz.com/public_html/wp-content/themes/mia luz/page-gallery.php
on line
23
Warning
: Invalid argument supplied for foreach() in
/home/mialuzco/domains/mia-luz.com/public_html/wp-content/themes/mia luz/page-gallery.php
on line
23
Basing on this piece of knowledge I should have got it worked by now, but nothing really works..
How can I display post gallery images src?
EDIT
My post
In my WordPress MySQL DB there is a meta_key in wp_postmeta table with the value mazda-certified. Additionally there is a meta_value associated with the meta_key that reads: "Yes" or "No".
I need to figure out how to display an image if and only if the meta_key=mazda-certified and the meta_key=yes.
Any help with this is tremendously appreciated.
I have been researching this for hours and unfortunately only pulling up information regarding UserMeta.
As I don't really know how want to display you image, my easiest way to achieve this, is to create a shortcode that you'll be able to place anywhere you want
In your functions.php file :
add_shortcode('certified-image', 'add_certified_image');
function add_certified_image($atts, $content = ""){
global $post;
if(get_post_meta($post->ID,'mazda-certified', true) == 'yes'){
$content = '<img src="image-yes.jpg"/>';
}
else{
$content = '<img src="image-no.jpg"/>';
}
return $content;
}
In your template :
echo do_shortcode('[certified-image]');
It's a short example, read more about add_shortcode on the codex to add attributes to the shortcode.
I am using Advanced Custom Fields PRO Version 5.3.7. I am trying to load a picture that is within my custom post type.
I have configured the ACF field for the custom post type and it works well for text related fields.
However, I am trying to display an image. I am using an adapted example from the acf documentation. Below you can find my code.
<div class="image">
</div>
My problem ist that on my site, I get the Array back instead of the single value:
Any suggestions what is wrong with my code?
I appreciate your replies!
Dont use the_field('box_image')['url'] because the_field() echoes directly everything and echoing an array outputs array(); Instead of this, try this:
<?php $img = get_field('box_image'); ?>
<?php if( $img ): ?>
<a href="<?php the_field('link'); ?>" rel="nofollow" class="am" target="_blank" >
<img class="b-lazy wp-post-image b-loaded" alt="<?php esc_attr_e( $img['alt'] ); ?>" src="<?php echo esc_url( $img['url'] ); ?>" width="230">
</a>
<?php endif; ?>
Whn you setting a ACF, you can select options "Return value" (array, url, id) change this as id if you want get different sizes (thumbnails), and URL if you want get original image url.
if you use ID option to get image as html tag you can use wp_get_attachment_image($id, $size); i always prefer to store only image ID.
I am converting a Bootstrap template into a WordPress theme.
For custom fields I am using the plugin Advanced Custom Fields. The issue is when I am trying to add an image with the help of Advanced Custom Fields, I get the following error:
Warning: Illegal string offset 'url' in C:\wamp64\www\my-site\wordpress\wp-content\themes\bootstraptowordpress\page-home.php on line 31
However when I am adding text through this plugin it shows no error.
I have no other plugins installed other than ACF.
Here is my code:
$home_page_logo = get_field('home_page_logo');
<div class="front_logo">
<?php if( !empty($home_page_logo)): ?>
<img src="<?php echo $home_page_logo['url']; ?>" alt="<?php echo $home_page_logo['alt']; ?>" />
<?php endif; ?>
</div>
There are three ways that an image field can be returned in ACF (array, URL, or ID). It sounds like your field is set to return the URL - which returns as a string.
Therefore, you need to access it like this:
<?php echo $home_page_logo; ?>
instead of this:
<?php echo $home_page_logo['url']; ?>
Alternatively, you can edit the set up for the field in your WordPress admin and configure it to return an image array instead of the URL:
If it's set to the array option, you can access the url like you are currently doing so, as well as access a range of other data pertinent to the image (such as its width and height, WordPress attachment ID, caption if entered, etc.)
It's better to use gravity forms plugin. You can to download here:
Rocket Genius Gravity Forms WordPress Plugin
But if want to fix your code, try to test if is array:
<?php
$yes = array('this', 'is', 'an array');
echo is_array($yes) ? 'Array' : 'not an Array';
echo "\n";
$no = 'this is a string';
echo is_array($no) ? 'Array' : 'not an Array';
?>
http://php.net/manual/function.is-array.php
I havve created a pod called teams and within this pod I created a field for uploading imagethat is called team_flag
I can echo the title of that row in a while loop, but not the file(image) link.
E.g. this code for title output is working nice:
<?php echo $teams->field( 'name' ); ?>
However, echoing link to the file is not:
<?php echo $teams->field( 'team_flag' ); ?>
Any advice how to fix it?
The .guid is not a reliable property to use. If you migrate your site to another domain, all your URLs will break.
You should use
wp_get_attachment_url( $teams->field( 'team_flag.ID' ) )
instead.
To clarify, #rclai89 is correct, it is better to use:
wp_get_attachment_url( $teams->field( 'team_flag.ID' ) )
For some new comers this might be a bit confusing.
What that line will do, is get the URL of the ID given from that field.
if you did a error log:
error_log(print_r($teams->field( 'team_flag.ID' ),1 ));
You can see why using .guid would give you the correct "url".
but it is best to let WP do the hard work in this case:
<?php echo wp_get_attachment_url( $teams->field( 'team_flag.ID' ) ); ?>
Will in fact be more efficient.
SOLUTION is to add guid at the end:** <?php echo $teams->field( 'team_flag.guid' ); ?>