Problem Adding Custom Field Images – Outputting Field Name?
-
I created custom fields (called ‘image1’, ‘image2’, etc) with an image path as the value field. The ‘image1_label’ field has a value of ‘1’, which will be linked to view the images in the lightbox. (See the numbers below ‘Process Sketches’ on the test page above.
I am using the documentation found here and on the developer’s website here.
With the above documentation, I added a functions.php file withe the following code:
<?php function bd_parse_post_variables(){ // bd_parse_post_variables function for WordPress themes by Nick Van der Vreken. // please refer to bydust.com/using-custom-fields-in-wordpress-to-attach-images-or-files-to-your-posts/ // for further information or questions. global $post, $post_var; // fill in all types you'd like to list in an array, and // the label they should get if no label is defined. // example: each file should get label "Download" if no // label is set for that particular file. $types = array('image' => 'no info available', 'file' => 'Download', 'link' => 'Read more...'); // this variable will contain all custom fields $post_var = array(); foreach(get_post_custom($post->ID) as $k => $v) $post_var[$k] = array_shift($v); // creating the arrays foreach($types as $type => $message){ global ${'post_'.$type.'s'}, ${'post_'.$type.'s_label'}; $i = 1; ${'post_'.$type.'s'} = array(); ${'post_'.$type.'s_label'} = array(); while($post_var[$type.$i]){ echo $type.$i.' - '.${$type.$i.'_label'}; array_push(${'post_'.$type.'s'}, $post_var[$type.$i]); array_push(${'post_'.$type.'s_label'}, $post_var[$type.$i.'_label']?htmlspecialchars($post_var[$type.$i.'_label']):$message); $i++; } } } ?>
Then I used the magic function to call the images and links:
<?php while (have_posts()) : the_post(); bd_parse_post_variables(); ?> <div class="post"> <div class="entry"> <?php the_content(); ?> <!-- Display the list of links --> <ul class="process"> <?php while(count($post_images) > 0): ?> <li> <a href="<?php echo array_shift($post_images); ?>" title="Process Sketches"> <?php echo array_shift($post_images_label); ?> </a> </li> <?php endwhile; ?> </ul>
Everything works, but it’s outputting the ‘image1’ and ‘image2’ custom field names above the content (where I called the bd_parse_post_variables(); ).
Does anyone know why it’s happening and how to remove this?
Thanks!
- The topic ‘Problem Adding Custom Field Images – Outputting Field Name?’ is closed to new replies.