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.
Related
I've searched and searched, but basically what I'm trying to do is this:
I have a custom post type where there is a custom field called "loop". What I would like to do is when this field is filled out, I want to use it's content as a template part. So, for instance:
Custom Field Loop: customloop
get_template_part( 'customloop')
So, when I run my query for this post, I basically need whatever's filled out in that custom field to then be thrown into the get_template_part() so that it pulls that particular loop.
Any thoughts on how to do this?
Thanks in advance.
Yes, we can do this with the get_post_meta() function, just make sure to use the correct key name.
$loop = get_post_meta( get_the_ID(), 'customloop', true );
if($loop):
get_template_part('customloop');
else:
get_template_part('loop');
endif;
I am trying to incorporate an image slider shortcode into a custom WordPress theme template, so that the client will be able to paste the shortcode into the custom field and the template will read this and display it correctly. I do not want the client to paste the shortcode in the body of the post, as the slider needs to be displayed outside of the post wrapper (at full browser width).
I don't know much about php, so would really appreciate any help with this!
The code I have so far to display the slider via the template is:
<?php echo do_shortcode("[metaslider id=27]"); ?>
And to display the output of custom fields I have:
<?php echo get_post_meta($post->ID, 'slider', true); ?>
Each of these works on its own, but I need to combine them, so that the client doesn't have to edit the template just to add the shortcode. So I am thinking something like this should work:
<?php echo do_shortcode("[<?php echo get_post_meta($post->ID, 'slider', true); ?>]"); ?>
... but it doesn't.
Many thanks in advance for any help with this.
C
Simply pass the return value of get_post_meta (which would contain the shortcode as far as I understood) to the do_shortcode function as an argument:
do_shortcode(get_post_meta($post->ID, 'slider', true));
I want meta description of currently executing page in the header.php file in wordpress.
Wordpress function "get_post_meta" is not useful for this. It only returns custom meta tags.
can anyone help me out.
I'm going out on a limb and assuming that you're generating meta description tags dynamically. If that's the case, this should get you what you need...
<?php
echo get_post_meta($post->ID, 'description', true);
?>
If, on the other hand, that's not what you're doing... that method will not work.
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.