I am having trouble accessing a custom field on my Wordpress page.
I have a field for a user to upload an image file. The theme that I am using then displays the value of that image as just the image ID, instead of an image link. I display it using {CUSTOM_FIELD_TestFile} in the page editor.
I want it to display the actual image though, not the ID.
So, I created a PHP snippet that runs on the page, to do the converting.
If I use <img src=" <?php echo wp_get_attachment_url( 1013 )?> "> and I have the image ID hard coded (1013), it works perfectly.
However, I have had no luck getting the value of 'TestFile' into the PHP snippet.
I double checked the user_meta table and the meta_key for this file is indeed TestFile.
The viewing of the page is done usually when a user is not logged in. So, get_current_user_id() isn't an option, which means, I also can't use get_user_meta().
If I use:
<?php
$postId = get_the_ID();
echo $postId;
$fileLink = get_metadata( "user", $postId, "TestFile");
echo $fileLink;
?>
I get "array" as a response. If I change it to get_metadata( "user", $postId, "TestFile", true); I get nothing.
The exact same results if I use get_post_meta($postId, 'TestFile'); or get_post_meta($postId, 'TestFile', true);.
Am I on the completely wrong track? Is there an easier way to do this? Or is there something that I should be doing with that "array" value that is being returned.
Thanks so much for any help you can offer.
If you have added a custom field called TestFile in the editor, you should be able to get that in your template with:
$value = get_post_meta($postId, 'TestFile', true);
I'm not sure what you mean about the user being logged in or out, because custom fields belong to the post and have nothing to do with the user.
Assuming that works, you should be able to add the ID to your image element:
<img src="<?= wp_get_attachment_url($value); ?>">
Related
I'm quite new to PHP but my desired functionality is to pull the image attachment URL from it's ID (generated through an ACF Custom Field) and use that as the src for an image.
This is the code that I have right now which should dynamically pull the image URL from the ID but doesn't seem to work:
<?php echo wp_get_attachment_url( the_field('upload_image') ); ?>
This is the code that works but isn't dynamic:
<?php echo wp_get_attachment_url( 257 ); ?>
Note the 257 is the ID of the image - I just need this to be able to be pulled dynamically by appropriating the first code snippet to take that attachment ID from the custom post field (which it should be doing in the first snippet but isn't).
Can anyone help out?
In acf the_field just output the item directly. In your case you need to use get_field function to get the id of the uploaded image. Because get_field returns the item.
I'm trying to link posts from WordPress to an associated post on Tumblr. I have the Tumblr id set as the value for a custom field in each WordPress post.
Here's what makes sense to me, but the link isn't capturing or outputting the value:
<h3><?php the_title(); ?></h3>
Is there a problem with the above code that anyone can see? Any help is, as always, greatly appreciated, thanks!
Also, I'm using the Ajax Load More plugin to load the posts to the page, not sure if that's why...
The meta key should be a string. You're not using $key so I'd remove that to avoid confusion.
Change this:
<?php $key="mykey"; echo get_post_meta($post->ID, tumblr_id, true); ?>
To:
<?php echo get_post_meta( $post->ID, 'tumblr_id', true ); ?>
Okay, so I realize what the problem is, because I'm using a function to load the posts to the page, global $post; needs to be declared within the loop.
Also, as #Nathan Dawson pointed out the meta key needed to declared as a string.
I'm trying to get a dynamic image to display the image defined on a variable, and also to have the link to that url to be the same as the permalink for the item.
echo '<img src="'.$thumburl.'" alt="Status">';
Any idea what I'm doing wrong with that code?
code use on your loop:
<?php
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumbnail');
?>
<?php echo '<a href="'.get_permalink().'"><img src="'.$image_url[0].'"
alt="Status"></a>'; ?>
You need to use get_permalink() instead of the_permalink()
echo '<img src="'.$thumburl.'" alt="Status">';
get_permalink():
Returns the permalink to a post or page for use in PHP. It does NOT display the permalink and can be used outside of The Loop. On failure returns false.
the_permalink():
Displays the URL for the permalink to the post currently being processed in The Loop. This tag must be within The Loop, and is generally used to display the permalink for each post, when the posts are being displayed. Since this template tag is limited to displaying the permalink for the post that is being processed, you cannot use it to display the permalink to an arbitrary post on your weblog. Refer to get_permalink() if you want to get the permalink for a post, given its unique post id.
You don't seem to tell us what the problem is. I suppose that the image is not displayed. The code is OK.
Double check the image path stored in $thumburl. Inspect the element in a browser to check that img's source and post it here. Are you using an absolute path in $thumburl?
I am currently posting from the front end of wordpress using code I found here
http://voodoopress.com/review-of-posting-from-front-end-form/
however I am using a few plugins that have thier fields in meta boxes on the backend that I can't seem to pass the data to from the front end form.
One of the plugins is the wordpress facebook plugin http://wordpress.org/extend/plugins/facebook/
The field id like to have access to is the suggest-friends ID as it has ajax facebook auto complete for current friends as you type.
Is this a possibility on the front end or am I attempting the impossible?
Thanks
If I understand your question right, this is the function you need:
http://codex.wordpress.org/Function_Reference/get_post_meta
The usage example they give in the documentation is this:
<?php $meta_values = get_post_meta($post_id, $key, $single); ?>
For the first value, $post_id, you can follow this instruction:
$post_id is the ID of the post you want the meta values for. Use
$post->ID to get a post's ID within the $post variable scope. Use
get_the_ID() to retrieve the ID of the current item in the WordPress
Loop
So if you are on your template page $post->ID would be fine.
Next, you need $key to be "suggest-friends"
Finally, $single set as true returns a string
So you can use this as your final result:
<?php
$suggestFriends = get_post_meta($post->ID, 'suggest-friends', true);
echo 'The value of suggest-friends for this post is: '.$suggestFriends;
?>
Hope that helps!
I've just followed this example from Wordpress and I have successfully added an extra Meta Box in Post interface, and the value is stored in DB.
Now my question is, how can I retrieve and display the content of this meta box?
I'm trying the following code:
$intro = get_post_meta($post->ID, 'post_intro', true);
echo $intro;
But I get nada. What am I doing wrong?
And while I'm here, does anybody know if I can place this extra meta box above the default text box in Wordpress post page?
is your snippet within the loop? If so use get_the_ID() instead of $post->ID.
it should look the this:
$intro = get_post_meta(get_the_ID(), 'post_intro', true);
echo $intro;
If you need to get you meta data outside the loop do this:
global $post;
$intro = get_post_meta($post->ID, 'post_intro', true);
echo $intro;
The reason you were getting nothing is because you have to globalize the $post variable if you want to access it. Always use the first method unless you have no choice. If your trying you use meta data for page templates please say so because I have a better solution for handling meta data in that situation.
Good luck!
As a side note I'd like to reference a WordPress Meta Box PHP Helper class that might help you and others when working with WordPress Meta Boxes.