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.

This is the statement of post submit:*

<fieldset>        
  <label for=MarketItem_cost><?php echo __('Software Version', 'WpForest'); ?>:</label>
  <p><input type="text" size="50" class="do_input"  name="soft" value="<?php echo get_post_meta( $pid, 'WpForest_soft', true ); ?>" /> 
  </p>
</fieldset> 

This is the statement on sidebar display:

<tr>
  <td class="attr-name"><?php echo __('Software Version', 'WpForest'); ?></td>
  <td class="attr-detail"><?php if ( get_post_meta( $post->ID, 'WpForest_soft', true )) : ?> </td>
</tr>

What I want is that, if any value is input on post form, then it will appear on the sidebar but when input is blank it will not show.

Right now it will show everything on sidebar, where there is value on input given or not.

How to code it with my present coding stated above.

share|improve this question
    
It's impossible read you question. Can you edit it please? –  JMau May 5 '13 at 15:09
    
I would say this: if(false !== get_post_meta( $post->ID, 'WpForest_soft', true )) but I'm not quite sure of what you want exactly. –  JMau May 5 '13 at 15:16
    
How can someone input if the field isn't there? Your question presents a paradox. –  user32437 May 5 '13 at 15:38
add comment

1 Answer

up vote 0 down vote accepted

Try using this:

// Get the 'WpForest_soft' custom field value.
$text = get_post_meta( $post->ID, 'WpForest_soft', true );

// Print the value and surrounding markup only if the value tests true.
if ( $text )
    printf( '<tr><td class="attr-name">%s</td><td class="attr-detail">%s</td></tr>', __( 'Software Version', 'WpForest' ), $text );

General Case

To do the above with any optional markup follow these steps.

1) Place the custom field value of in a variable. Use a relevant variable name.

// Get the 'WpForest_soft' custom field value.
$wpforest_soft = get_post_meta( $post->ID, 'WpForest_soft', true );

2) Write a PHP conditional testing the value from step 1.

if ( $wpforest_soft ) {
}

3) Add your markup to that conditonal.

if ( $wpforest_soft ) {
    ?>
    <a href="<?php bloginfo('url'); ?>/theme-demo/<?php echo the_ID(); ?>" class='blue-btn right ml10 live-preview' target='_blank'>Live Preview</a>
    <a href="<?php bloginfo('url'); ?>/theme-demo/<?php echo the_ID(); ?>" class='blue-btn right ml10 live-preview' target='_blank'>Video Preview</a>
    <a href="<?php bloginfo('url'); ?>/screenshots/<?php echo the_ID(); ?>" class='blue-btn right ml10 screenshots' target='_blank'>Screenshots</a>
    <?php
}

4) (Optional) Avoid jumping in and out of PHP and Markup. The printf() PHP function can help.

if ( $wpforest_soft ) {
    printf(
        '<a href="%1$s/theme-demo/%2$s" class="%3$s" target="_blank">Live Preview</a>' .
        '<a href="%1$s/theme-demo/%2$s" class="%3$s" target="_blank">Video Preview</a>' .
        '<a href="%1$s/screenshots/%2$s" class="%4$s" target="_blank">Screenshots</a>',

        get_bloginfo( 'url' ),
        get_the_ID(),
        'blue-btn right ml10 live-preview',
        'blue-btn right ml10 screenshots'
    );
}

Or just:

if ( $wpforest_soft )

    printf(
        '<a href="%1$s/theme-demo/%2$s" class="%3$s" target="_blank">Live Preview</a>' .
        '<a href="%1$s/theme-demo/%2$s" class="%3$s" target="_blank">Video Preview</a>' .
        '<a href="%1$s/screenshots/%2$s" class="%4$s" target="_blank">Screenshots</a>',

        get_bloginfo( 'url' ),
        get_the_ID(),
        'blue-btn right ml10 live-preview',
        'blue-btn right ml10 screenshots'
    );
share|improve this answer
    
"This answer was automatically flagged as low-quality because of its length and content", which I am sure you don't want. Please try to explain what your code does in human readable terms. –  s_ha_dum May 8 '13 at 4:22
    
Thanks Charles it works good. –  mus720714 May 9 '13 at 3:07
    
Hai Charles Clarkson i need one more help,i want to do the same for video preview,screenshots and live preview but it come with button.How to code using my statement below. –  mus720714 May 9 '13 at 7:28
    
<a href="<?php bloginfo('url'); ?>/theme-demo/<?php echo the_ID(); ?>" class='blue-btn right ml10 live-preview' target='_blank'>Live Preview</a><a href="<?php bloginfo('url'); ?>/theme-demo/<?php echo the_ID(); ?>" class='blue-btn right ml10 live-preview' target='_blank'>Video Preview</a><a href="<?php bloginfo('url'); ?>/screenshots/<?php echo the_ID(); ?>" class='blue-btn right ml10 screenshots' target='_blank'>Screenshots</a> –  mus720714 May 9 '13 at 7:30
    
@mus720714 See edit. –  Charles Clarkson May 9 '13 at 9:11
show 7 more comments

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.