WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] check if any custom field with certain name has value (5 posts)

  1. Beee
    Member
    Posted 3 years ago #

    I'm trying to show content, when either one of four custom fields is used.

    I'm trying to show a link to the online shop for a certain record if it's entered. I have 4 custom fields available for this.

    I have this

    <p class="buy">Buy online:
    	<?php $key = "beatport"; $bietport = get_post_meta($post->ID, $key, true); if ($bietport) { ?><a href="<?php $key="beatport"; echo get_post_meta($post->ID, $key, true); ?>">Beatport</a><?php } ?>
    	<?php $key = "junodownload"; $juno = get_post_meta($post->ID, $key, true); if ($juno) { ?><a href="<?php $key="junodownload"; echo get_post_meta($post->ID, $key, true); ?>">Juno</a><?php } ?>
    	<?php $key = "rushhour"; $rh = get_post_meta($post->ID, $key, true); if ($rh) { ?><a href="<?php $key="rushhour"; echo get_post_meta($post->ID, $key, true); ?>">Rush Hour</a><?php } ?>
    	<?php $key = "discogs"; $dg = get_post_meta($post->ID, $key, true); if ($dg) { ?><a href="<?php $key="discogs"; echo get_post_meta($post->ID, $key, true); ?>">Discogs</a><?php } ?>
    </p>

    I've tried this code but doesn't work. It always gives Yes, even if no values are entered. It works with 1 value, but when more values come into play, it doesn't anymore...

    <?php
    	$bietport = get_post_meta($post->ID, $beatport, true);
    	$juno = get_post_meta($post->ID, $junodownload, true);
    	$rh = get_post_meta($post->ID, $rushhour, true);
    	$dg = get_post_meta($post->ID, $discogs, true);
    	if ($bietport || $juno || $rh || $dg) { ?>
    		YES
    <?php } else { ?>
    		NO
    <?php } ?>

    The first part should come where it says YES and no will be removed (if I get it working)
    Hoping somebody knows an answer...

  2. Beee
    Member
    Posted 3 years ago #

    Basicy this is the line that causes the error

    if ($bietport || $juno || $rh || $dg) { ?>

    it works if I use

    if ($bietport) { ?>

    but I need to check all fields

  3. Beee
    Member
    Posted 3 years ago #

    Found my own solution

    if (!$bietport && !$juno && !$rh && !$dg) { ?>
  4. Beee
    Member
    Posted 3 years ago #

    doesn't work entirely

  5. Beee
    Member
    Posted 3 years ago #

    Now I have it :) finally...

    the first if statement was correct. it was above which was incorrect.

    $bietport = get_post_meta($post->ID, $beatport, true);

    should be

    $bietport = get_post_meta($post->ID, beatport, true);

    (without $ sign before key)

Topic Closed

This topic has been closed to new replies.

About this Topic