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' ); ?>
Related
I'm trying to get the image meta of my thumbnails in WordPress, but it just keeps failing. I have no idea why is that. Reading the documentation I can't really see what I'm doing wrong. See (https://codex.wordpress.org/Function_Reference/wp_read_image_metadata). How would you do this?
This is my code basically:
$url = get_the_post_thumbnail_url();
var_dump(wp_read_image_metadata($url));
All this does is to return bool(false)
Check the code below and let me know if it works for you.
<?php
$feat_image = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
echo $feat_image;
?>
I have ACF Plugin installed and I have a gallery filed in my post. I've tried all these docs but still getting the error :
Invalid argument supplied for `foreach()`
this happens because the input of the for each is not an array!
Do you have any clue what's wrong with this?
Do you think if I have to set something while I've defined the custom field?
<?php
$images = get_field('mygall');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<?php echo wp_get_attachment_image( $image['ID'], $size ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I think your problem comes from the fact that you are using get_field() instead get_fields(). That's way you don't get an array.
If it still doesn't work check the documentation for get_fields() here. Try to debug it like using only get_fields() and see what is the output. If it is an empty array then it means that you are calling the function out of the loop and it can't get the post id. So do a second test with manually setting the post id like get_fields(123); and check the results. If there are no results then there is something wrong with that post. And if there are results then you can do a final test with checking what will be the result of get_fields(123, 'gallery').
All the above debugging can be wrapped in something like:
echo '<pre>';
print_r( get_fields(123) );
echo '</pre>';
Basically this will give you some idea of what is the structure of the data that you get from this function and how you can manipulate it so to get what you need.
I am using a plugin that outputs data with a simple tag reference
<?php echo $property['building_brochure']; ?>
This outputs the reference number for an uploaded pdf which in this example is 352.
I want to return the actual link for this, the following works
<?php echo wp_get_attachment_url( 352 ); ?>
However, I want the combine these so on the single page template for the entry the relevant PDF is called i.e.
<?php echo wp_get_attachment_url( 'building_brochure' ); ?>
Is there a straightforward way of doing this?
You need to place the function as input into the other function the way Spectre-d has shared in comments.
i.e
<?php echo wp_get_attachment_url( $property['building_brochure'] ); ?>
However if you face issues,like not correct attachment_URL, then it could be Type issue i.e. first function might be outputting string and not integer as expected by attachment_url function in that case I recommend:
<?php
$pdf_id = (int)$property['building_brochure'];
echo wp_get_attachment_url( $pdf_id );
?>
I'm not very familiar with PHP and have been trying my hardest to figure out how to create this URL. So far, this is working:
<?php echo site_url($p->post_title) ?>
Where post title is defined by the Mapify.it Wordpress plugin. The result is:
http://siteurl.com/post_title
What I'd like to do is add a string before it, ideally ?s= or /search/, but when I try to add this before $p->post_title I'm still generating the above URL. Variations such as:
<?php echo site_url('?s=', $p->post_title) ?>
<?php echo site_url('/search/', $p->post_title) ?>
produce http://siteurl.com/?s= and ignore the variable. Nothing seems to do what I want.
What am I doing wrong?
Hope you need the following url format,
http://siteurl.com/?s=Here come the post title
So,
<?php echo site_url("?s=".$p->post_title) ?>
OR
<?php echo site_url("/search/".$p->post_title) ?>
should work.
Found it!
<?php echo site_url('?s='), $p->post_title ?>
Instead of adding custom URL Parameters directly, I'd suggest you to use WordPress built-in function add_query_arg(), it's more cleaner.
Here is an usage example:
$url = get_site_url();
$params = array(
's' => $p->post_title
);
echo add_query_arg($params, $url);
You can specify multiple parameters this way.
For ref: Check add_query_arg()
I have define an option in theme option for option tree plugin.
Option id is banner_list, the type is list item.
I do not know how to retrieve the option on my theme.
I typed
<?php
if ( function_exists( 'ot_get_option' ) ) {
$banner_list = ot_get_option( 'banner_list' );
}
echo $banner_list; ?>
,
but the output is array.
How should I write the code?
I have no idea what exactly you are looking for concerning your plugin for wordpress. What I do know, is that the variable you're trying to echo is in fact an array.
I you want to know the array values your array is holding I'd suggest trying:
<pre>
<?php print_r($banner_list); ?>
</pre>
This should give you an overview of the keys your array is holding as well as the values of those keys. After that you can either echo them out out individually or in a foreach loop.
More information concerning arrays and/or loops:
http://www.tizag.com/phpT/arrays.php
http://www.tizag.com/phpT/foreach.php
http://www.w3schools.com/php/php_looping_for.asp
http://www.w3schools.com/php/php_arrays.asp
Hope this will help you in the right direction.
All the best and happy holidays!
Jesper
Pretty easy! Here is the solution:
<?php
$banner_list = get_option_tree( 'banner_list ', '', false );
?>
If you need to retrieve data from custom meta boxes, use below code:
<?php
$header_image= get_post_meta($post->ID, 'my_header_image', true);
?>