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 create a function to add data from two custom fields after the entry title. I get it to show correctly when these fields are filled in. The fields are a image source url and an url the image has to link to.

The problem is that it's taking up space even when the custom fields are empty. My php skills are not good enough to work out how to change this. I have added a check but now the fields are not showing up at all anymore.

Hope anybody can help me out with this. What I got so far is:

//Add custom field image after entry title
function headway_title_button()
{
    $check = get_post_meta($post->ID, 'titel_knop');
    if( !empty( $check[""] ) )
    {
        return printf(
            '<div class="title-btn"><a href="%s" alt="%s"><img src="%s" /></a></div>'
            ,get_post_meta( get_the_ID(), 'titel_knop_url', true )
            ,get_the_title()
            ,get_post_meta( get_the_ID(), 'titel_knop', true )
        );  
    }
    else
    {
        return printf('<div style="display:none"></div>');
    }
}

add_action( 'headway_after_entry_title', 'headway_title_button' );
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

There are a few issues that break your code:

  • $post doesn't exist, you'd need to import it using global, or simply use get_the_ID as you're already doing
  • $check[''] won't return anything since it will access an undefined index, outside of the empty construct it would trigger a warning

I can't test it right now, but this should work, at least syntax wise (and according to the API documentation theoretically also functionality wise):

function headway_title_button()
{
    $post_id = get_the_ID();

    if(metadata_exists('post', $post_id, 'titel_knop'))
    {
        printf(
            '<div class="title-btn"><a href="%s" alt="%s"><img src="%s" /></a></div>',
            get_post_meta($post_id, 'titel_knop_url', true)
            get_the_title(),
            get_post_meta($post_id, 'titel_knop', true)
        );  
    }
}

However it might be more efficient to fetch all metadata at once. But WordPress isn't smart enough to unserialize it when not requesting a single value, so you would need to do that on your own (if neccessary at all, depends on the metadata):

function headway_title_button()
{
    $meta = get_post_meta(get_the_ID());

    if(!empty($meta['titel_knop'][0]))
    {
        printf(
            '<div class="title-btn"><a href="%s" alt="%s"><img src="%s" /></a></div>',
            maybe_unserialize($meta['titel_knop_url'][0]),
            get_the_title(),
            maybe_unserialize($meta['titel_knop'][0])
        );  
    }
}
share|improve this answer
    
Thanks so much. This did the trick! –  Marja Oct 9 '12 at 0:52
add comment

Change,

if( !empty( $check[""] ) )

To,

if( $check != '' )

In full,

//Add custom field image after entry title
function headway_title_button()
{
    $check = get_post_meta($post->ID, 'titel_knop', true);
    if( $check != '' )
    {
        return printf(
            '<div class="title-btn"><a href="%s" alt="%s"><img src="%s" /></a></div>'
            ,get_post_meta( get_the_ID(), 'titel_knop_url', true )
            ,get_the_title()
            ,get_post_meta( get_the_ID(), 'titel_knop', true )
        );  
    }
    else
    {
        return printf('<div style="display:none"></div>');
    }
}

add_action( 'headway_after_entry_title', 'headway_title_button' );

Notes

You can also do,

if( !empty( $check ) )

But be aware that if the custom field happens to contain,

  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)

The conditional !empty() will evaluate as FALSE, meaning that the above are also treated as empty values when using PHP's empty() function.

While its perfectly acceptable to use, its worthwhile knowing how it evaluates values.

There's also isset() which can be used in the same fashion,

if ( isset ( $check ) )

But in the case of isset() its behavior varies. Again, perfectly acceptable to use, however $check != '' is my recommendation in this instance.

share|improve this answer
add comment

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.