WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Separator symbol between custom fields (6 posts)

  1. ddorr
    Member
    Posted 1 year ago #

    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

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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);
    } ?>
  3. ddorr
    Member
    Posted 1 year ago #

    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

  4. vtxyzzy
    Member
    Posted 1 year ago #

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

    The separator only shows if both fields are present.

  5. ddorr
    Member
    Posted 1 year ago #

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

    Thanks again Vtxyzzy

  6. ddorr
    Member
    Posted 1 year ago #

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic