WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Echoing div when post meta has content (11 posts)

  1. WillBG
    Member
    Posted 1 year ago #

    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?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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; ?>
  3. WillBG
    Member
    Posted 1 year ago #

    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

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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 ) != '' ) {
  5. WillBG
    Member
    Posted 1 year ago #

    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

  6. keesiemeijer
    moderator
    Posted 1 year ago #

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

  7. WillBG
    Member
    Posted 1 year ago #

    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.

  8. WillBG
    Member
    Posted 1 year ago #

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

  9. keesiemeijer
    moderator
    Posted 1 year ago #

    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>';
  10. WillBG
    Member
    Posted 1 year ago #

    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; ?>
  11. WillBG
    Member
    Posted 1 year ago #

    This has been resolved

Topic Closed

This topic has been closed to new replies.

About this Topic