• I have been working on developing a site for about a month now and have been using the Beta version to get things up and running knowing that I wanted to use Ver 3.0 when it became available. But since upgrading it seems my templates don’t pull in custom field data anymore.

    I was using the code below to list out ALL custom fields for a particular key. The key being “Product_image”. So it would build an list of images for a gallery and all I had to do was just put the file name of the jpeg in the custom field for each post. Worked perfectly in the Beta 2 of 3.0, but has since stopped working.

    <?php if (have_posts()) : ?>
                   <?php while (have_posts()) : the_post(); ?>    <?php
    $press_quotes = get_post_meta($post->ID, "Product_image", false);
    if ($press_quotes[0]=="") { ?>
    
    <!-- If there are no custom fields, show nothing -->
    
    <?php } else { ?>
    
    	<?php foreach($press_quotes as $press_quotes) {
    	echo '<li><img src="/10-images/product-images/'.$press_quotes.'"alt="product"/></li>';
    	} ?>
    
    <?php } ?>            <?php endwhile; ?>
         <?php endif; ?>

    Any ideas on how I can fix this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • So is $press_quotes empty even when you’ve specified a “Product_image” custom field?

    try adding <!-- $press_quotes = <?php print_r($press_quotes); ?> --> save, reload the post, and view source to see if $press_quotes is really empty

    foreach($press_quotes as $press_quotes)

    — also, I’ve never tried assigning the same variable twice like that…maybe your foreach is not running?

    Thread Starter pablo631

    (@pablo631)

    It still came up empty, even though I tested it pulling the field using a wp_query and that worked fine. I solved it another way by getting custom post values.

    <?php
    
      $mykey_values = get_post_custom_values('image');
      foreach ( $mykey_values as $key => $value ) {
        echo "<li><img src='/10-images/product-images/$value.jpg'/></li>";
      }
    
    ?>

    The only issue is it displays an error if the custom field is not present. Which is fine for what I want. But any idea on how I would have it check first. Then if the custom field is not present display alternate content?

    If print_r($press_quotes); comes up empty then there’s no custom field with that name for the post (else it would have data).

    For your conditional code..

    $mykey_values = get_post_custom_values('image');
    if( $mykey_values && '' != $mykey_values ) {
    	// Values found
    }
    else {
    	// Nothing found, do something else
    }

    You need to specify the page id along with the key in get_post_custom_values().

    Should look something like this:

    get_post_custom_values($key=’image’, $post_id=$post->ID)

    If you’re inside the loop it’s not actually required.

    Outside the loop, or anywhere where the function can’t pull the post/page ID you’ll need to supply it as postoy suggested.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Problem with Getting Custom Fields in Ver 3.0???’ is closed to new replies.