WordPress.org

Ready to get started?Download WordPress

Forums

Display or Hide custom field if active and limit characters (8 posts)

  1. Fanaticweb
    Member
    Posted 2 years ago #

    Hello,

    This is a 2 in 1 request attempt.

    1. I'm trying to pull the values of a specific custom field named (status_value) located within custom posts generated by the subscribers and show them within the loop_posts page one after the other based on the condition that IF the user filled up the status_value field, display it, if not, don't.

    I took a look at the following codex link explaining the custom fields and my guess is that I will be using something similar to:
    <?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?>

    In my case, I tried:
    <b>Status:</b> <span class="status_value"><?php echo get_post_meta($post->ID, 'status_alert', true); ?></span>

    This works fine for pulling the custom field value, but I'm stuck with the static display of the wording "Status:" and I need it displayed IF the value is filled up or or else display none.

    2. The second challenge is to limit the amount of characters of the outputed value of "status_value" given it will be displayed within a div of 480px width.
    I found this thread and it seemed logical to combine it with the 1st step, but how to blend both functions?

  2. David Choi
    Member
    Posted 2 years ago #

    Maybe something like

    <?php
    $status_value = get_post_meta($post->ID, "status_value", true);
    if ( !empty($status_value) ) { // if not empty, then do something
    .... do something .....
    }
    ?>

  3. Fanaticweb
    Member
    Posted 2 years ago #

    Thats the thing, I'm able to pull and display the value of the custom field when it's filled by the user, but when it's not, I still see "Status:" given I placed <b>Status:</b> before the code, the "...do something..." you're proposing is what I'm missing, furthermore, I don't think I should be even displaying that <b>Status:</b> all together unless I can control its display pending on the value of the custom field.

    I'm thinking maybe a css class that would display: none IF the custom field is empty, but how to go by placing it in the code.

    P.S.: I'm not a coder, I'm learning here.

    By the way, nice social theme you got, seen it on TF

  4. David Choi
    Member
    Posted 2 years ago #

    Maybe like this:

    <?php $status_value = get_post_meta($post->ID, "status_value", true); ?>
    <?php if ( !empty($status_value) ) { ?>
        <b>Status:</b>
        <span class="status_value"><?php echo $status_value; ?></span>
    <?php } ?>

    shorter version can be

    <?php if ($status_value = get_post_meta($post->ID, "status_value", true) ) { ?>
        <b>Status:</b>
        <span class="status_value"><?php echo $status_value; ?></span>
    <?php } ?>
  5. Fanaticweb
    Member
    Posted 2 years ago #

    THAT WORKED!! :)
    I used the first one and I just had to close the last span> to display correctly, thank you so much!

    Any idea how I can combine it with limiting the length of the custom field value?
    I know it should be something similar to this code used for the Title of the post but how do I go by and use it for the custom field?
    if (mb_strlen(get_the_title()) >= 75) echo mb_substr(get_the_title(), 0, 75)

  6. David Choi
    Member
    Posted 2 years ago #

    Glad it worked. To compare the string length, you can use strlen() function, like

    if ( strlen($title) > 10 ) {
        ... do something ...
    }

    to use for multibyes (foreign languages), you can use instead mb_strlen() like:

    if ( mb_strlen($title) > 10 ) {
        ... do something ...
    }

    here are the references:

    http://php.net/manual/en/function.strlen.php
    http://www.php.net/manual/en/function.mb-strlen.php

  7. Fanaticweb
    Member
    Posted 2 years ago #

    I'll play around with it and give it a try, thank you again for your time and help, much appreciated.

  8. David Choi
    Member
    Posted 2 years ago #

    Sure, you are welcome and have a nice day!

Topic Closed

This topic has been closed to new replies.

About this Topic