WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Echoing div when post meta has content

[Resolved] Echoing div when post meta has content

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Test if it’s not an empy string (something like this):

    <?php
    $meta = get_post_meta( $post->ID, 'rg_wp_editor_sample', true );
    if( $meta != '' ) :
    ?>
    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/bg-acc-top.png" style="margin: 0px 0 -10px 0;"/>
    <div id="accordion">
      <?php echo isset($meta['es']) ? apply_filters('the_content', $meta['es']) : 'no Meta Box meta saved'; ?>
    </div>
    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/bg-acc-bottom.png" style="margin: 0px 0 20px 0;"/>
    <?php endif; ?>

    Hi Keesie, the image borders are still displaying when the field is empty in the admin window. I managed it with this on a previous project

    <?php
         $post_meta = get_post_meta($post->ID, "Accordion", true);
         if (!empty($post_meta)) {
        ?>
    
    <img src="images/bg-acc-top.png"/>
    <div id="accordion">
    <?php echo $post_meta; ?>
    </div>
    <img src="images/bg-acc-bottom.png"/>
    <?php
    }
    ?>

    BUT I have a wysiwyg in the back end now so I have to echo it like the above in my question

    Moderator keesiemeijer

    @keesiemeijer

    Not sure what it is you want or what the problem is. Maybe echoing everything will help [untested]:

    <?php
    $meta = get_post_meta( $post->ID, 'rg_wp_editor_sample', true );
    if( $meta != '' ) {
      echo '<img src="' . get_stylesheet_directory_uri() . '/images/bg-acc-top.png" style="margin: 0px 0 -10px 0;"/>';
      echo '<div id="accordion">';
      echo isset($meta['es']) ? apply_filters('the_content', $meta['es']) : 'no Meta Box meta saved';
      echo '</div>';
      echo '<img src="' . get_stylesheet_directory_uri() . '/images/bg-acc-bottom.png" style="margin: 0px 0 20px 0;"/>';
    }
    ?>

    or maybe change this:

    if( $meta != '' ) {

    to this:

    if( trim( $meta ) != '' ) {

    Keesie,

    Please see this URL as you can see the red border images are still on page where as if you see the other pages there is content in that meta box. I need the borders to not display if the meta box (custom field ) is left empty

    Moderator keesiemeijer

    @keesiemeijer

    Maybe a silly question, but does the page with ID 19 (page_id=19) have a custom field “rg_wp_editor_sample”?

    Yes, if I enter some text in the custom field it echos on the page. Please see the page now for an example. I just need the image borders to be hidden if the custom field is left empty.

    Did you manage to find a solution Keesie? I am stumped…

    Moderator keesiemeijer

    @keesiemeijer

    Ah I see it’s an array? try it with this:

    $meta = get_post_meta( $post->ID, 'rg_wp_editor_sample', true );
    if(isset( $meta['es']) && $meta['es']) { ... rest of code ... }

    Try and test what the meta array holds by using this:

    $meta = get_post_meta( $post->ID, 'rg_wp_editor_sample', true );
    echo '<pre>';
    print_r($meta);
    echo '</pre>';

    Keesie you are a genius!

    Just so anyone else visits this thread with the same problem, this is my working code

    <?php
    $meta = get_post_meta( $post->ID, 'rg_wp_editor_sample', true );
    if(isset( $meta['es']) && $meta['es']) :
    ?>
    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/top.png" />
    <div id="accordion">
      <?php echo isset($meta['es']) ? apply_filters('the_content', $meta['es']) : 'no Meta Box meta saved'; ?>
    </div>
    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/bottom.png" />
    <?php endif; ?>

    This has been resolved

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Echoing div when post meta has content’ is closed to new replies.
Skip to toolbar