Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to add a value assigned in the custom field by doing a post in these following steps: I make a new post, add a new custom field where i specify a name and a value.

PHP

I add the following scripts to index.php loop exactly below where the while and its parameter begins and to the HTML where I want the field added respectively

<?php while ( have_posts() ) : the_post(); ?>
<?php the_meta();?>


<?php echo get_post_meta(get_the_ID(), 'key');?>

The hard part for me to understand if i am doing right is the HTML. when i launch the site using firefox and firebug, I detect the post and find the html where the field is supposed to be added. But I noticed that WP automatically generates paragraph tags for the original post, so i honestly dont know where i am supposed to add the get_post_meta for it to be added into my post.

share|improve this question
    
Please extend your code sample so we can see where you call the_content(). –  toscho Nov 5 '12 at 2:14

1 Answer 1

The update_user_meta and get_user_meta functions are what you want, without having to fool around with any database connections or SQL. All it takes is one include statement in your theme, and a bit of PHP magic to call the meta functions.

Check out my code, which accomplished what you are trying to do, on Github (https://github.com/yasyf/WP-Subscriptions/blob/master/subs.php). It should serve as a good example. I also have a description with embedded code on my blog: Using WordPress As A Pay-For-Access System.

Alternatively, you can check out how I added code to my footer to show text based on custom fields (in this case, hours remaining in a free trial). Looking at what you tried to do, you might add similar code to your theme's header.php instead.

<?php
$current_user = wp_get_current_user();
$subscription = get_user_meta($current_user->ID, 'subscription', true);
if($subscription != "true")
        {
$time = (80000 - (intval(time()) - intval($_COOKIE['seshCurrent'])))/3600;
if(intval($time) < 0)
{
    $time = 22.3;
}
echo("$time hours remaining in this session");
}
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.