• Resolved WillBG

    (@willbg)


    Hi all,

    I need to wrap my post meta content in a div with top and bottom borders (that are images). I have the code (CODE 1) but it doesn’t display any post meta at all whereas the second bit of code (CODE 2) does but when the custom meta is empty it leaves the border images on page

    CODE 1

    <?php $meta = get_post_meta( $post->ID, 'rg_wp_editor_sample', true );
    	if (!empty($post_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
    	}?>

    CODE 2

    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/bg-acc-top.png" style="margin: 0px 0 -10px 0;"/>
                    <div id="accordion">
                    	<?php $meta = get_post_meta( $post->ID, 'rg_wp_editor_sample', true );
    		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;"/>

    Any ideas how to echo the whole Div including images only when the custom meta contains information in the back end?

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

    Thread Starter WillBG

    (@willbg)

    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 ) != '' ) {

    Thread Starter WillBG

    (@willbg)

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

    Thread Starter WillBG

    (@willbg)

    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.

    Thread Starter WillBG

    (@willbg)

    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>';

    Thread Starter WillBG

    (@willbg)

    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; ?>
    Thread Starter WillBG

    (@willbg)

    This has been resolved

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