• Hello,
    I’m trying to learn how things work in WP and got stumbled on this one.

    You see, I’m trying to stylize my wp theme especially on its custom fields. this is what i did:

    <div class="cfield">
    <?php the_meta(); ?>
    </div>

    obviously it worked just fine whenever a user fill the custom field (or when an admin create a new key+value). but what i don’t know is when a user did not fill any custom fields, you’ll see a blank ‘box’ in the template. the blank ‘box’ that was created by the <div class=”cfield”> is still there without anything inside it (of course).

    is there a way to make the <div class=”cfield”> (and other related styling) hidden when a user left the custom fields blank?

    do i have to edit this (post-template.php)?

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

Viewing 7 replies - 1 through 7 (of 7 total)
  • Never edit WordPress core files

    Use the function to test to see if the post has any metadata, then output only if it has!

    <?php if( the_meta() ) : ?>
       <div class="cfield">
          <?php the_meta(); ?>
       </div>
    <?php endif; ?>

    HTH

    David

    Thread Starter qlooney

    (@qlooney)

    i tried your suggestion but unfortunately the class=”cfield” box is still there. i may be missing something, since i’m not familiar with codes like this.

    i’ll try to play around with the code based on your suggestion. thanks for the reply.

    Thread Starter qlooney

    (@qlooney)

    hmm.. i tried to modify the cfield class, but it turned out that the one’s causing the box to appear was the post-meta class. looks like i made a mistake there.

    your suggestion did work. now the problem is the post-meta class. ??

    Same principal applies but with your div nested!

    <?php if( the_meta() ) : ?>
       <div class="post-meta">
          <!-- any other html code -->
          <div class="cfield">
             <?php the_meta(); ?>
          </div>
       </div>
    <?php endif; ?>

    HTH

    David

    Thread Starter qlooney

    (@qlooney)

    sorry for the stupid questions, because i really have no knowledge at these kind of things please bear with me. but i think i’m beginning to understand the logic in your code.

    one more question, wasn’t the post-meta class already included in the code inside post-template.php? i mean this one:
    echo "<ul class='post-meta'>\n";

    when using the previous code, i think it was that particular line that displayed the blank box.

    Yes it is a part of the code function, so it means that post meta is returning something.

    This line means that it must be finding keys with no value!

    if ( $keys = get_post_custom_keys() ) {

    HTH

    David

    Thread Starter qlooney

    (@qlooney)

    ok. i’ll try to figure out the rest from here. if failed, than i’ll be back. haha.

    thanks so much for the help. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘styling the_meta()’ is closed to new replies.