• gregory9885

    (@gregory9885)


    This is basically to credit photo sources at the end of single.php. If no custom field is filled in, then it should return nothing. If there’s 1 field filled in, then return 1 field. If there’s 2, then return both fields and so on. I’m using this for custom fields.

    I’m putting this at the beginning of my loop because that’s what’s required from the tutorial I’m using…

    <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); $data = get_post_meta( $post->ID, 'key', true ); ?>

    This is the (admittedly) hacked together If Else statement..

    <?php $cfield1 = get_post_meta( $post->ID, 'photo1', true ); ?>
    <?php $cfield2 = get_post_meta( $post->ID, 'photo2', true ); ?>
    <?php $cfield3 = get_post_meta( $post->ID, 'photo3', true ); ?>
    <?php $cfield4 = get_post_meta( $post->ID, 'photo4', true ); ?>
    <?php $cfield5 = get_post_meta( $post->ID, 'photo5', true ); ?>
    <?php if($cfield1) { ?>
    	<a><href="<?php echo $cfield1; ?>">Photo Source (1)</a>
    <?php } if($cfield2) { ?>
    	<a><href="<?php echo $cfield2; ?>">Photo Source (2)</a>
    <?php } if($cfield3) { ?>
    	<a><href="<?php echo $cfield3; ?>">Photo Source (3)</a>
    <?php } if($cfield4) { ?>
    	<a><href="<?php echo $cfield4; ?>">Photo Source (4)</a>
    <?php } if($cfield5) { ?>
    	<a><href="<?php echo $cfield5; ?>">Photo Source (5)</a>
    <?php } else { ?>
    NOTHING
    <?php } ?>

    Any help would be greatly appreciated!

Viewing 10 replies - 1 through 10 (of 10 total)
  • luckdragon

    (@luckdragon)

    the way you have that written, it checks to see if photo1 is defined, if it is, it shows, it

    then, is photo 2 defined, if yes, show it
    then, is photo 3 defined, if yes, show it
    then, is photo 4 defined, if yes, show it
    then, is photo 5 defined, if yes, show it if photo 5 isn’t defined “NOTHING”

    if that’s what you’re trying to do, then you don’t need the }else{ line at all

    also, if that’s what you’re trying to do, using an array might be easier

    $cfield = array();
    for ($i=1;$i<6;$i++) {
      $ok = get_post_meta($post->ID, 'photo'.$i, true );
      if ($ok) $cfield[$i] = $ok;
    }
    if (count($cfield) > 0) {
      foreach ($cfield as $key => $val) { ?>
    <a href="<?echo $val ?>">Photo Source (<? echo $key ?>)</a>
    <? } } ?>
    Thread Starter gregory9885

    (@gregory9885)

    I’m still learning php, so I’m not too great at it. What you gave me still isn’t working for me πŸ™

    Does it have something to do with the additional $data = get_post_meta( $post->ID, 'key', true ); that I added to the beginning of the loop?

    luckdragon

    (@luckdragon)

    what is “key” in your get_post_meta?

    Thread Starter gregory9885

    (@gregory9885)

    I’m not exactly sure what my key is. That may be the problem I’ve got.

    I’m using this for custom fields in my functions.php.

    The beginning portion of my functions.php looks like this: http://pastebin.com/2PnMCiQh

    luckdragon

    (@luckdragon)

    you do realize that WP has custom fields built in now by default, don’t you?

    Thread Starter gregory9885

    (@gregory9885)

    I know that they’re there, but I need a box to always show up every time an author edits a post. That’s not currently available is it?

    luckdragon

    (@luckdragon)

    if you click on “screen options” and check the box that says “extra fields”, it should be (I think)

    Thread Starter gregory9885

    (@gregory9885)

    Yes, I knew that was there. Thanks for the tip though. I wanted to use a custom write panel though, which is what I was using from the tutorial located here: http://wefunction.com/2009/10/revisited-creating-custom-write-panels-in-wordpress/

    I just can’t seem to get that working with the If Else statement that I have above πŸ™

    luckdragon

    (@luckdragon)

    well, part of that if else statement that you have above is coded wrong, you have

    <href

    not <a href

    Thread Starter gregory9885

    (@gregory9885)

    Wooooow, I didn’t even realize that. Thank you!

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Confused with this If Else statement for Custom Fields’ is closed to new replies.