Empty custom fields are printing, but I don't want them to.
-
I’m trying to call some hidden custom fields into a meta box. The custom fields are the same – a maximum of five named _link and five named _url, with sequential numbers following (eg _link3). However, if the user has only filled in one link when submitting the post, I don’t want the empty fields to appear.
I have knocked up the following loop. In theory, it should do a get_post_meta for _link0, and if it isn’t empty it should do the same for _url1, print the results, and advance the counter to process _link1/_url1. With _link0/_url0 contained in the database, those fields appear fine. What I’m finding is that because _link1/_url1 aren’t in the database, the loop will print the first field correctly, followed by four more empty fields.
I’m stumped. I’ve seen posts that suggest using !empty($link) and found that while it prints the first fields fine, the second fields will cause a fatal error (maximum execution time exceeded) and stop the page from loading. My code is below – am I overlooking something simple?
$i = 0; do { $link_key = '_link' . $i; $url_key = '_url' . $i; $link = get_post_meta($post->ID,$link_key,TRUE); if (isset($link)) { $url = get_post_meta($post->ID,$url_key,TRUE); include(MY_PLUGIN_FOLDER . 'links.php'); $i++; } else { $i = 5;} } while ($i<=5);
links.php:
<div>Link Name: <input type="text" name="link<?php echo $i; ?>" id="link<?php echo $i; ?>" value="<?php echo $i; ?>" /> URL: <input type="text" name="url<?php echo $i; ?>" id="url<?php echo $i; ?>" value="<?php echo $i; ?>" /><input type="button" class="addnew" value="Add Link" /></div>
- The topic ‘Empty custom fields are printing, but I don't want them to.’ is closed to new replies.