• Hello wordpress community.

    In my single post php, I added a table…

    <table border="1" id="personal">
    <caption STYLE="text-align:left"><h2>Personal Information</h2></caption>
    <tr>
    <th width="19%">Gender</th><th width="16.9%">Birth Year</th><th>Current Location</th><th>Availability</th>
    </tr>
    <tr>
    <td><?php $key="gender";
    if(get_post_meta($post->ID, $key, true)): ?>
    <p><?php echo get_post_meta($post->ID, $key, true); ?></p>
    <?php endif;?>
    </td>
    <td><?php $key="birth_year";
    if(get_post_meta($post->ID, $key, true)): ?>
    <p><?php echo get_post_meta($post->ID, $key, true); ?></p>
    <?php endif;?></td>
    <td><?php $key="current_location";
    if(get_post_meta($post->ID, $key, true)): ?>
    <p><?php echo get_post_meta($post->ID, $key, true); ?></p>
    <?php endif;?></td>
    <td><?php $key="availability";
    if(get_post_meta($post->ID, $key, true)): ?>
    <p><?php echo get_post_meta($post->ID, $key, true); ?></p>
    <?php endif;?></td>
    </tr>
    </table>

    [please use the code button instead of blockquote to mark any code – https://codex.www.ads-software.com/Forum_Welcome#Posting_Code ]

    how do i hide this table using php if there are no values in the custom fields?

    THANK YOU AHEAD OF TIME!

Viewing 5 replies - 1 through 5 (of 5 total)
  • check for the custom values, and only if any of them is set, show the table;

    example:
    https://pastebin.com/E77gaUNN

    Thread Starter aimkbe

    (@aimkbe)

    @alchymyth

    Thank you so much, i used your example for all my tables, but i have a issue with one. I think it’s because some of the value is an array…

    <?php
    $certificates = get_post_meta($post->ID, “certificates”, true);
    $credential/license_ = get_post_meta($post->ID, “credential/license_”, true);
    $single_subject = get_post_meta($post->ID, “single_subject”, true);
    if( $certificates || $credential/license_ || $single_subject ) : ?>
    <table border=”1″ id=”certification”>
    <caption STYLE=”text-align:left”><h2>Certificates/Credentials</h2></caption>
    <tr>
    <th width=”19%”>Certificates</th><td><?php $key=”certificates”;
    if(get_post_meta($post->ID, $key, true)): ?>
    <p><?php get_post_meta($post->ID, $key, true); the_field($key); ?></p>
    <?php endif;?></td>
    </tr>
    <th>Credentials</th>
    <td><?php $key=”credential/license_”;
    if(get_post_meta($post->ID, $key, true)): ?>
    <p><?php get_post_meta($post->ID, $key, true); the_field($key); ?>
    <?php endif;?> <?php $key=”single_subject”;
    if(get_post_meta($post->ID, $key, true)): ?>:
    <?php get_post_meta($post->ID, $key, true); the_field($key); ?></p>
    <?php endif;?></td>
    </table>
    <?php endif; ?>

    Thank you again alchymth

    $credential/license_ is not a valid variable name;
    try to change it to $credential_license
    keep the key unchanged;

    full changed top section:

    <?php
    $certificates = get_post_meta($post->ID, "certificates", true);
    $credential_license = get_post_meta($post->ID, "credential/license_", true);
    $single_subject = get_post_meta($post->ID, "single_subject", true);
    if( $certificates || $credential_license || $single_subject ) : ?>

    or rewrite the same section in this way:
    (no purpose to use variables if you don’t re-use them in the rest of the code)

    <?php if( get_post_meta($post->ID, "certificates", true) || get_post_meta($post->ID, "credential/license_", true) || get_post_meta($post->ID, "single_subject", true) ) : ?>
    Thread Starter aimkbe

    (@aimkbe)

    DAMMIT @alchymyth !!!

    THANK YOU SO MUCH!

    Thread Starter aimkbe

    (@aimkbe)

    hello @alchymyth

    I have one more question, hopefully the last.

    BTW your advice for hiding the tables worked like a charm, thank you.

    Now, i wanted to add a more tag before the tables. I know in the edit post page all you need to do is add

    <!–more–>

    but that only blocks the content. How would i add that in my single.php to activate the more tag for the tables?

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘how do i hide my html table if there are no value’ is closed to new replies.