Support » Fixing WordPress » Separator symbol between custom fields

  • Resolved ddorr

    (@ddorr)


    Hello

    This is my code for display custom fields (brand, city):

    <?php if ( get_post_meta($post->ID, 'brand', $single = true) != '' ) { ?>
    	<?php echo get_post_meta($post->ID, 'brand', $single = true) ?> |
    	<?php } ?>
    <?php if ( get_post_meta($post->ID, 'city', $single = true) != '' ) { ?>
    	<?php echo get_post_meta($post->ID, 'city', $single = true) ?> |
    	<?php } ?>

    my question is how can I hide the symbol separator | after first custom field if only have that custom field?

    Thanks.

    ddorr

Viewing 5 replies - 1 through 5 (of 5 total)
  • vtxyzzy

    (@vtxyzzy)

    Give this a try:

    <?php $sep = '';
    if ( get_post_meta($post->ID, 'brand', $single = true) != '' ) {
    	echo get_post_meta($post->ID, 'brand', $single = true);
    	$sep = ' | ';
    }
    if ( get_post_meta($post->ID, 'city', $single = true) != '' ) {
    	echo $sep;
    	echo get_post_meta($post->ID, 'city', $single = true);
    } ?>
    Thread Starter ddorr

    (@ddorr)

    Vtxyzzy thanks a lot for you code. it works very well. But now I see what when don’t have the second custom fields (city) the separator appears also.

    Thanks

    vtxyzzy

    (@vtxyzzy)

    Are you sure you copied the code correctly? It seems to work for me.

    The separator only shows if both fields are present.

    Thread Starter ddorr

    (@ddorr)

    oh I am embarrassed. I check the code again and saw my mistake. The code it works perfect.

    Thanks again Vtxyzzy

    Thread Starter ddorr

    (@ddorr)

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Separator symbol between custom fields’ is closed to new replies.