• On my blog (Bon Bon), I am currently using this bit of code to display a custom field that I manually insert a link into, on posts that I have that use affiliate links:

    <?php $affiliates = get_post_meta ($post->ID, 'affiliates', true); ?>
    <?php if (strlen ($affiliates) > 0): ?>
        <div class="awrap"><?php echo $affiliates; ?></div>
    <?php endif; ?>

    However, I would like to do something a bit more sophisticated, and display something more along the lines of this:

    <?php $affiliates = get_post_meta ($post->ID, 'affiliates', true); ?>
    <?php if (strlen ($affiliates) > 0): ?>
        <div class="awrap"><?php echo "<a href="/./affiliates">affiliate link</a>"; ?></div>
    <?php endif; ?>

    By doing so, I want to display a simple <a href="/./affiliates">affiliate link</a> regardless of what the value of the custom field is, so long as the custom field $affiliates has value.

    I wasn’t able to get the above to execute how I like (i.e., it wound up displaying on every post, which isn’t good, because only some of my posts have affiliate links.

    Help is greatly appreciated!

    All the best,
    Ashley

Viewing 1 replies (of 1 total)
  • The code you posted has a syntax error – improperly nested quotes. Try this:

    <?php $affiliates = get_post_meta ($post->ID, 'affiliates', true); ?>
    <?php if (strlen ($affiliates) > 0): ?>
        <div class="awrap"><?php echo '<a href="/./affiliates">affiliate link</a>'; ?></div>
    <?php endif; ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Display a specific when custom field has value’ is closed to new replies.