WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Separator symbol between custom fields

[Resolved] Separator symbol between custom fields

  • 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)
  • 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);
    } ?>

    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

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

    The separator only shows if both fields are present.

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

    Thanks again Vtxyzzy

    Thanks

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