WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Only display custom field if present and add content filters (4 posts)

  1. flint_and_tinder
    Member
    Posted 2 years ago #

    Hi all,

    I'm trying to display the contents of a custom field with auto p tags if the content field has been added. So far I have this but it doesn't work, can anyone help with what I'm doing wrong please?

    <?php if ( get_post_meta($post->ID, 'credits', true) ) : ?>
            <aside class="credits">
              <?php echo apply_filters('the_content', $credits); ?>
            </aside>
            <?php endif; ?>
  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with this [untested]:

    <?php
    $credits = get_post_meta($post->ID, 'credits', true);
    if ($credits != ''  ) : ?>
            <aside class="credits">
              <?php echo apply_filters('the_content', $credits); ?>
            </aside>
            <?php endif; ?>
  3. flint_and_tinder
    Member
    Posted 2 years ago #

    Ignore the above, I got myself in a pickle. The code below works ok.

    <?php if( $credits = get_post_meta($post->ID, 'credits', true) ): ?>
            <aside class="credits">
              <?php echo apply_filters('the_content', $credits); ?>
            </aside>
    <?php endif; ?>
  4. flint_and_tinder
    Member
    Posted 2 years ago #

    Thanks Keesiemeijer, it seems your code works as well.

Topic Closed

This topic has been closed to new replies.

About this Topic