I want custom fields to only appear when they have a value

Hi,

I have a number of custom fields being displayed in a single-custompress.php file, and I want them to only appear when they have a file. Right now I have two text blocks for each custom field, one to display the title and one to display the value.

I want to only display the title when there is a value in the field.

Does that make sense?

Heres a sample of the code I'm using.

<?php //Named Title
                        echo do_shortcode('[ct id="ct_Subsidiari_textarea_b4d5" property="title"]'); ?>
                        <br />
                        <?php //Named value
                        echo do_shortcode('[ct id="ct_Subsidiari_textarea_b4d5" property="value"]'); ?
  • Vaughan

    Hi Harry,

    Hope you're well?

    I haven't tried this, but hopefully, something like below should work:

    <?php //Named Title
    $display_field = do_shortcode('[ct id="ct_Subsidiari_textarea_b4d5" property="value"]');
    if($display_field !== '') {
        echo do_shortcode('[ct id="ct_Subsidiari_textarea_b4d5" property="title"]'); ?>
        <br />
        <?php //Named value
            echo do_shortcode('[ct id="ct_Subsidiari_textarea_b4d5" property="value"]');
    }
    ?>

    Hope this helps

  • Harry

    Hi Vaughan,

    Thank you! That worked great all all but one case of custom posts.

    I want to have a link to a download, so I used a text field custom post, then used the value of that custom field as the hyperlink on the front end. Now though with this $display_field I can't seem to produce the same results.

    Any idea?

    Here is the code I was using before

    <?php //File download
                            echo do_shortcode('[ct id="ct_Updated_Di_text_0920" property="title"]'); ?>
                            <br />
                             <a href="<?php echo do_shortcode('[ct id="ct_Updated_Di_text_0920" property="value"]'); ?> " target="_blank">Click to Download</a>
  • Harry

    HI Vaughan,

    Im not sure if the new code works with it because I think I must have my syntax wrong because they page doesn't load at all when I try it.

    Here is the existing code black that I'm trying with $display_field but the page is just white and doesn't load.

    <?php //File download
    $display_field = do_shortcode('[ct id="ct_Updated_Di_text_0920" property="value"]');
    if($display_field !== '') {
    echo do_shortcode('[ct id="ct_Updated_Di_text_0920" property="title"]'); ?>
    <br />
    <a href="<?php echo do_shortcode('[ct id="ct_Updated_Di_text_0920" property="value"]'); ?> " target="_blank">Click to Download</a>
    }
    ?>
  • Vaughan

    Hi,

    Yeah, you missed an opening php tag (it's why I like putting them on seperate lines.)

    <?php //File download
    $display_field = do_shortcode('[ct id="ct_Updated_Di_text_0920" property="value"]');
    if($display_field !== '') {
        echo do_shortcode('[ct id="ct_Updated_Di_text_0920" property="title"]');
    ?>
    <br />
    <a href="<?php echo do_shortcode('[ct id="ct_Updated_Di_text_0920" property="value"]'); ?> " target="_blank">Click to Download</a>
    <?php
    }
    ?>

    Hope this helps

  • Harry

    Yes that works! Thank you. I have another question on displaying shortcodes. I want to add a line space to the taxonomy short codes, but inside the shortcake so it doesn't add line breaks when no content is displayed.

    Is there a way to do this?

    Here is the current code I have. it will still display the
    even if there is no content.

    <?php // Country
                            echo do_shortcode('[tax id="country" before="Countries involved in this investigation: " separator=", " after="]');?>
                            <br /><br />

    Thanks so much for all your help!!