• I created some custom meta fields that have a background image attached to each field via the use of divs in css, however, when it posts, all the backgrounds show up even if there’s nothing entered in the fields.

    Is it possible to write a loop that specifies a field to show up only if it has data in it, otherwise, nothing shows, not even the div?

    I’m so confused!

Viewing 8 replies - 1 through 8 (of 8 total)
  • I answered something similar in an earlier post:

    https://www.ads-software.com/support/topic/help-making-a-conditional-download-button?replies=2

    However you may want to checkout the function that is closer to your needs which is get_post_custom().

    https://codex.www.ads-software.com/Function_Reference/get_post_custom

    Just add code to check the returned array has specific entries or not and use these results to either display divs or not. Once you have the custom post fields returned successfully it is fairly straightforward to do what you need.

    Thread Starter angelakwong

    (@angelakwong)

    hmm. i might be in over my head…

    Just add code to check the returned array has specific entries or not and use these results to either display divs or not. Once you have the custom post fields returned successfully it is fairly straightforward to do what you need.

    how do you check if the returned array has specific entries?

    Well just as a test you can dump the results to the screen, for each post, just to check you are getting the correct values back from the post custom meta fields.

    Add (probably to the single.php theme template page) something like:

    $custom_fields = get_post_custom(); // for the current post
    echo '<pre>';
    print_r($custom_fields);
    echo '</pre>';

    If this is working and outputting the correct custom fields for each post (click on different posts to see the field output for each one) then you can then just use a loop (foreach) to go through all the custom fields and output as necessary.

    Thread Starter angelakwong

    (@angelakwong)

    So i ran the test, and this is what shows up….

    Array
    (
    [_edit_last] => Array
    (
    [0] => 1
    )
    [inks] => Array
    (
    [0] =>
    )
    [adhesives] => Array
    (
    [0] => this is an adhesive
    )
    [coatings] => Array
    (
    [0] =>
    )
    [_edit_lock] => Array
    (
    [0] => 1284757365
    )
    )

    Array
    (
    [_edit_last] => Array
    (
    [0] => 1
    )
    [_edit_lock] => Array
    (
    [0] => 1284757232
    )
    [website_url] => Array
    (
    [0] =>
    )
    [developers] => Array
    (
    [0] => this would be the adhesive info
    )
    [designers] => Array
    (
    [0] => this would be the ink info
    )
    [producers] => Array
    (
    [0] => this would be the coatings info
    )
    [inks] => Array
    (
    [0] => this is an ink
    )
    [adhesives] => Array
    (
    [0] => adhesive info
    )
    [coatings] => Array
    (
    [0] =>
    )
    )

    Well this means nothing to me really as I don’t know what format your custom fields are in. You need to check this output against what has been entered in the custom field meta box in the post edit screen.

    If it matches for each post, then great, you can then loop through the returned custom field array and check for a non-null value and output as you were doing before. But now you will have a conditional output. ??

    Ok lets say you created a custom field called custom_title, and on the posts where you have set a calur you want to output the text

    You could use inside the loop:

    <?php if(get_post_meta($post->ID, 'custom_title', true)) : ?>
    <h2>echo <?php get_post_meta($post->ID, 'custom_title', true) ;?></h2>
    <?php endif; ?>

    Line one is a conditional if statement, does our custom field have a value : = start, if it has a value then next line, else skip to endif.

    $post->ID is the unique index number of the post
    ‘custom_title’ is the name of our custom field
    true tells WordPress to only return the one field
    <h2> is heading style 2 </h2>
    echo outputs the value into the page
    endif; closes the block of code.

    You posted while I was typing!

    developers

    <?php if(get_post_meta($post->ID, 'developers', true)) : ?>
    <b>Developers: </b><?php echo get_post_meta($post->ID, 'developers', true) ;?>
    <?php endif; ?>
    
    <?php if(get_post_meta($post->ID, 'designers', true)) : ?>
    <b>Developers: </b><?php echo get_post_meta($post->ID, 'designers', true) ;?>
    <?php endif; ?>
    
    <?php if(get_post_meta($post->ID, 'website_url', true)) : ?>
    <b>Website: </b><?php echo get_post_meta($post->ID, 'website_url', true) ;?>
    <?php endif; ?>

    Gives you just two lines as you did not have a value in website_url
    Developers: this would be the adhesive info
    Designers: this would be the ink info

    This will give you more control where you may want a section header.

    HTH

    David

    If you need an example, this is one I was playing with last year, contact me and I will send you the code to add a set of defaults to every post.

    HTH

    David

    Thread Starter angelakwong

    (@angelakwong)

    David, that would be awesome.

    I’m trying to figure out how to target each of the custom fields via css…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Loop help?’ is closed to new replies.