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 was hoping for some assistance with a custom meta box that I added to my genesis child theme.

The data is being saved properly, and stored properly, but when I try to echo it out using get_post_meta, nothing shows up. But a custom function someone showed me does echo it out. Can someone figure out what it isn't working?

This should work but doesn't.

add_action ('genesis_before_post_content', 'gteh_tagline');
function gteh_tagline() {
    $meta = get_post_meta($post->ID, $field['dbt_text'], true);
    echo $meta;
    }

This works but I don't want to use this, would rather use the proper code

add_action ('genesis_before_post_content', 'gteh_tagline');
function gteh_tagline() {
    echo "<pre>";
    $customs = get_post_custom(get_the_ID());
    $text = (isset($customs['dbt_text'][0]))?$customs['dbt_text'][0]:"";
    var_dump($customs);
    echo "<br/>text=".$text;
    echo "</pre>";
}

It dumps this and echoes it out when asked.

["dbt_text"]=>
array(1) {
[0]=>
string(89) "At vero eos et accusamus et iusto odio dignityr simos ducimus qui blanditiis praesentium "
}

Here is dbt_text

$prefix = 'dbt_';
$meta_box = array(
'id' => 'my-meta-box',
'title' => 'Post Tagline',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
    array(
        'name' => 'Post Tagline',
        'desc' => 'Displayed below the post title',
        'id' => $prefix . 'text',
        'type' => 'text',
        'std' => ''
    )
)

);

share|improve this question

3 Answers 3

There is nothing 'improper' about the second block of code. Its just written for debugging purposes. The key difference is that that second block used get_the_ID(). Try that.

add_action ('genesis_before_post_content', 'gteh_tagline');
function gteh_tagline() {
    $meta = get_post_meta(get_the_ID(), $field['dbt_text'], true);
    echo $meta;
}

Or try to pull in $post with global.

add_action ('genesis_before_post_content', 'gteh_tagline');
function gteh_tagline() {
    global $post;
    $meta = get_post_meta($post->ID, $field['dbt_text'], true);
    echo $meta;
}

Or, depending on how that action is written, this might work.

add_action ('genesis_before_post_content', 'gteh_tagline');
function gteh_tagline($post) {
    $meta = get_post_meta($post->ID, $field['dbt_text'], true);
    echo $meta;
}
share|improve this answer
    
aah, didn't see your answer before i added one.. –  Pontus Abrahamsson Dec 22 '12 at 18:34
    
:) @PontusAbrahamsson No worries. I've done that a dozen times. –  s_ha_dum Dec 22 '12 at 18:35
    
Odd. I tried using get_the_ID() before posting this. It didn't work. I tried adding global $post; gteh_tagline($post) also does nothing. I am stumped. THe data is obviously being stored. –  gteh Dec 22 '12 at 18:47
1  
Does var_dump($post); or var_dump(get_the_ID()); give you anything with any of those combinations? –  s_ha_dum Dec 22 '12 at 19:20

You need to set $post to global within a function:

function wpse_16722_tagline() {
    global $post;

    $meta = get_post_meta( $post->ID, $field['dbt_text'], true );
        echo $meta;
    }
add_action ('genesis_before_post_content', 'wpse_16722_tagline');
share|improve this answer
    
Thanks I tried adding global $post; but it didn't make a difference. plus the function is being called from inside the loop. –  gteh Dec 22 '12 at 18:45
    
This function is hooked, not called within a loop, though that may be where the hook executes. I am not trying to be nit-picky. Just suggesting that you have scope issues you are not thinking about. –  s_ha_dum Dec 22 '12 at 19:21

Two problems with the first function:

  1. Use get_the_ID() or global the $post before using $post->ID.

  2. $field['dbt_text'] isn't defined in the function anywhere. Is this supposed to be coming from a global as well?

Edit: After reading your comments and replies to the other answers, I think you don't understand how PHP's variable scoping works. You may want to read up on this in order to understand how variables work inside functions:

http://php.net/manual/en/language.variables.scope.php

share|improve this answer
    
$field['dbt_text'] is defined elsewhere in functions.php for meta box. I'll add it to the top. I've tried get_the_ID() and global $post and neither work. –  gteh Dec 23 '12 at 5:19
    
Anything "defined elsewhere" is not defined when you're inside a function. Functions have their own variable scope in PHP. You need to get this value from wherever you're getting it from inside every function, or you need to put it into a global variable and then declare that you want to bring that global variable into the function scope inside the function. –  Otto Dec 23 '12 at 21:19
    
BTW, get_the_ID() and/or using the global $post do work, but you're not getting the results you want because $field is not defined inside that function. You need to correct both problems at the same time to get the function to work. –  Otto Dec 23 '12 at 21:22

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.