Grow your CSS skills. Land your dream job.

[WORDPRESS] Remove div IF textarea is empty for Events Post Type Function

  • # June 13, 2013 at 11:06 pm

    I followed [this tutorial.](http://tatiyants.com/how-to-use-wordpress-custom-post-types-to-add-events-to-your-site/ “http://tatiyants.com/how-to-use-wordpress-custom-post-types-to-add-events-to-your-site/”)

    Added a custom textarea to the meta box for “Events” the information is displayed on my events page but my issue now is that when there is no text inside of the textarea the label “INFO” is still visible. I’d like to remove the label if no information has been added.

    Here is the code for the custom text area//

    $ret = $ret . '

    ';

    and here is the code I used for the html within the function//

    $ret .= ‘[div class="event-section"]‘;
    $event_info = get_post_meta($post->ID, ‘event_info’, true);
    if (!empty($event_info)) {
    $ret .= ‘INFO:   ‘ . $event_info . ‘‘ ;
    } $ret .= ‘[/div]‘;

    Any ideas?
    Please help…
    Thanks in advance!

    # June 13, 2013 at 11:37 pm

    My WordPress setup was similar. I changed yours to what mine looked like which worked for me.

    < ?php
    $event_info = get_post_meta($post->ID, ‘event_info’, true);
    if(!empty($event_info)): ?>
    INFO: < ?php echo $event_info; ?>
    < ?php endif; ?>

    # June 14, 2013 at 12:46 am

    @chrisburton Sorry I forgot to mention this code was inside of my functions.php file.

    # June 14, 2013 at 12:52 am

    @Keilowe Why wouldn’t you put it in your template file?

    # June 14, 2013 at 1:20 am

    @chrisburton The tutorial I linked to is an Events CPT, and inside of it, the author created a shortcode that holds the date, location, ticket price and event information.

    So on my wordpress Events page all I have to add add this short code//

    [events daterange="current"]

    # June 14, 2013 at 1:39 am

    @keilowe I don’t know if this will help but try removing the space between `if (…`

    # June 14, 2013 at 1:46 am

    @chrisburton :( No it didn’t work…I appreciate your effort to help me!

    # June 14, 2013 at 1:53 am

    @keilowe Try this

    $ret .= ‘[div class="event-section"]‘;
    $event_info = get_post_meta($post->ID, ‘event_info’, true);
    if($event_info != ”) {
    $ret .= ‘INFO:   ‘ . $event_info . ‘‘ ;
    } $ret .= ‘[/div]‘;

    # June 14, 2013 at 1:58 am

    @chrisburton Nope :/
    Would it help to post the section of my CPT?

    # June 14, 2013 at 4:46 pm

    Anyone?

    # June 14, 2013 at 4:59 pm

    Could you share a gist or pen of the CPT file and the shortcode function?

    for the if statement try this

    if(!strlen(trim($even_info) ) == 0) {
    // code to spit out info
    }

    # June 14, 2013 at 6:07 pm

    @mcjohnst Here is [the short code function.](http://pastebin.com/LkK67GXd “http://pastebin.com/LkK67GXd”)

    # June 14, 2013 at 7:03 pm

    @chrisburton I placed my Shortcode function inside of [this pastebin.](http://pastebin.com/LkK67GXd “http://pastebin.com/LkK67GXd”)

    If you wanted to give it a looksie?

    # June 14, 2013 at 7:45 pm

    This is a bit over my head. When it comes to PHP, I only know what I’ve had to do for my own website. I’ve never dealt with shortcodes when I used WordPress for my CMS. Maybe @traq could chime in.

    # June 14, 2013 at 7:52 pm

    @chrisburton Okay :) Thank you for trying to help though!

Viewing 15 posts - 1 through 15 (of 33 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".