WP adds empty <ul></ul> even when there is no custom field data
-
Hi!
I started using Custom Fields with my entries not so long ago, following the tutorial and adding <?php the_meta(); ?> to my templates at the right places. There was no problem, it perfectly showed up. I only used it for simply adding things like “Currently reading” and so.The problem is that even when I don’t add any custom fields to my entry, WP still inserts an empty unordered list to the HTML. (
<ul class="post-meta"></ul>
)
I looked into wp-includes/post-template.php and wp-includes/post.php, becausethe_meta()
is defined in the former andget_post_custom_keys()
in the latter.
According to the comments in post.php,get_post_custom_keys()
returns null if there was no custom field to be find.the_meta()
supposedly examines whether that value is 0, but it doesn’t seem to work. I suspected thatget_post_custom_keys()
‘s return value is not 0 and started forcing it to return so, but there was no result. I just don’t get it why does the empty list appear.Here are the definitions of the two functions:
function get_post_custom_keys( $post_id = 0 ) { $custom = get_post_custom( $post_id ); if ( !is_array($custom) ) return; if ( $keys = array_keys($custom) ) return $keys; }
function the_meta() { if ( $keys = get_post_custom_keys() ) { echo "<ul class='post-meta'>\n"; foreach ( (array) $keys as $key ) { $keyt = trim($key); if ( '_' == $keyt{0} ) continue; $values = array_map('trim', get_post_custom_values($key)); $value = implode($values,', '); echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value); } echo "</ul>\n"; } }
The XHTML standard doesn’t approve of empty lists and IE shows the styling for it even when there is no data inside so this ended up being a bit annoying. I’d be glad if anyone could help, I’m okay with PHP but don’t know much about how the arrays work.
All in all, what I’d like to have is WP to not insert an empty
<ul></ul>
when there is no data in Custom Field! Thanks in advance!
- The topic ‘WP adds empty <ul></ul> even when there is no custom field data’ is closed to new replies.