The MU forums have moved to WordPress.org

Hiding 'Custom Fields' keys (14 posts)

  1. boonika
    Member
    Posted 15 years ago #

    Hi there.

    I don't know how to hide unused (when no value is entered) keys. Here's the code I use inside single.php:

    <ul class='post-meta'>
    				<a>Client:</a> <?php
    				// this is where the custom field prints info about the Client for who the work was created
    				$values = get_post_custom_values("Client"); echo $values[0]; ?> |
    				<a>Year:</a> <?php
    				//this is where the custom field prints info about when the work was created
    				$values = get_post_custom_values("Year"); echo $values[0]; ?> |
    				<a>Tools:</a> <?php
    				//this is where the custom field prints info about how the work was created
    				$values = get_post_custom_values("Tools"); echo $values[0]; ?>
    			</ul>
  2. dsader
    Member
    Posted 15 years ago #

    Would if(is_callable($values[0])) echo $values[0]; do anything?
    or
    if(isset($values[0])) echo $values[0];
    or
    if($values[0]) echo $values[0];`

  3. lunabyte
    Member
    Posted 15 years ago #

    !empty would be my recommendation.

  4. boonika
    Member
    Posted 15 years ago #

    @dsader & lunabyte

    Thanks a lot. Will try all of them.

  5. boonika
    Member
    Posted 15 years ago #

    Looks like the problem is in me (not for the first time). Sorry for wasting your time. I've formulated my question totally wrong.

    What I'm trying to hide, when no value is entered, are these parts of code: <a>Client:</a>, <a>Year:</a> and <a>Tools:</a>. Previously I sad "keys". Wrong. I got confused cause keys have same names. But when I look at it again it looks like I wrote it totally wrong cause placing those words inside <a></a> is not the smartest idea to display key names... and than again, I don't know no other way that actually works.

    What I want to achieve is this. When all the values are entered line of text bellow the post should look like this:

    Client: Automattic | Year: 2008 | Tools: Zbrush, Photoshop

    and when some values aren't entered (value for 'Client' for example) it should look like this:

    Year: 2008 | Tools: Zbrush, Photoshop

  6. dsader
    Member
    Posted 15 years ago #

    Example

    $values = get_post_custom_values("Client");
    if(!is_empty($values) { ?>
    <a>Client:</a>
    <?php
    echo $values[0];
    } ?>
  7. boonika
    Member
    Posted 15 years ago #

    @dsader
    Well, first I got blank screen with error messages because of { in line 2 of your code. Than I changed it to:

    $values = get_post_custom_values("Client");
    if(!is_empty($values)) ?>
    <a>Client:</a>
    <?php
    echo $values[0];
    ?>

    My page showed up but another error message said that !is_empty is undefined function... or something like that. Than I changed it to:

    $values = get_post_custom_values("Client");
    if(isset($values[0])) ?>
    <a>Client:</a>
    <?php
    echo $values[0];
    ?>

    No more error messages but still displaying "Client:" when no value is entered. Also tried if(!is_empty($values)). Same thing.

    Will try with if($values[0]). Maybe it will work:) Thanks for your effort.

    [EDIT]
    And yes, I did delete old <a>Client:</a> before <?php

    :)

  8. dsader
    Member
    Posted 15 years ago #

    Sorry, !empty.

  9. boonika
    Member
    Posted 15 years ago #

    Nope! Same story. No errors but still displaying "Client:" when no value is entered.

    <?php
    // this is where the custom field prints info about the Client for who the work was created
    $values = get_post_custom_values("Client");
    if(!empty($values)) ?>
    <a>Client:</a>
    <?php
    echo $values[0];
    ?>

    Maybe <a>Client:</a> is not the right way to display key name. I think it's got something with get_post_custom_keys(). Don't bother, I'll try to solve it by myself. Thanks.

  10. boonika
    Member
    Posted 15 years ago #

    This looks promising. I'll explore how functions are defined in this plugin:

    http://coffee2code.com/archives/2008/04/01/get-custom-field-values-v25/

  11. dsader
    Member
    Posted 15 years ago #

    $values is the array, it won't be empty unless no items are there at all. You need a "foreach" loop to check the item for empty.

    http://codex.wordpress.org/Function_Reference/get_post_custom_values

  12. boonika
    Member
    Posted 15 years ago #

    Thanks dsader. I'm closing this topic:D

  13. boonika
    Member
    Posted 15 years ago #

    Ok, opening it again... I know I said 'I'm closing this topic' but I also said a lot of other things in my life.

    Anyway, this plugin does exactly what I want. But the problem is in me again. I don't want to use a plugin, I just want to add few lines of code inside my theme's template.

    So this is how it works with this plugin. You have to paste this code inside the loop:

    <?php echo c2c_get_custom('Client', 'Client: '); ?>

    Which displays, if value is 'WordPress', the following:
    Client: WordPress

    And that's it. When no value is entered nothing get's displayed. Not even the key name. My little brain tried all kinds of combinations to figure out how c2c_get_custom works for empty values. No luck. My guess is this but I can't get it to work:

    if ( empty($value) ) {
    	if ( empty($none) ) return;
    	$value = $none;
  14. boonika
    Member
    Posted 15 years ago #

    EDIT: Deleted:)

About this Topic