• 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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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 …..
    }
    ?>

    Thread Starter Fanaticweb

    (@fanaticweb)

    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

    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 } ?>
    Thread Starter Fanaticweb

    (@fanaticweb)

    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)

    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:

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

    Thread Starter Fanaticweb

    (@fanaticweb)

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

    Sure, you are welcome and have a nice day!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display or Hide custom field if active and limit characters’ is closed to new replies.