• Resolved sueblack

    (@sueblack)


    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, because the_meta() is defined in the former and get_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 that get_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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter sueblack

    (@sueblack)

    Ugh, these things happen, after writing for help you’ll find the solution by yourself.
    Definately not the most elegant solution, but it did the trick for me. I modified the the_meta() to be like this:

    function the_meta() {
    	if ( $keys = get_post_custom_keys() ) {
    		$smgv = 0;
    		foreach ( (array) $keys as $key ) {
    			$keyt = trim($key);
    			if ( '_' == $keyt{0} )
    				continue;
    			if ( $smgv == 0 ) {
    				echo "<ul class='post-meta'>\n";
    				$smgv = 1;
    			}
    			$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);
    		}
    		if ( $smgv == 1 ) {
    			echo "</ul>\n";
    		}
    	}
    }

    My conclusion: despite the comments in post.php get_post_custom_keys() does NOT return NULL when there is no custom data.

    I won’t delete this thread, maybe it will help someone in the future.

    Thank you, I’m so glad you didn’t delete this!
    Exactly what I needed, hopefully this will be added in future versions.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP adds empty <ul></ul> even when there is no custom field data’ is closed to new replies.