-1

I am having an issue with the below code when it comes to displaying a div based on a specific metabox is filled in. I am using the Group metabox selection from CMB2 to handle my metaboxes. As of right now when the newblock_url is filled in it is only displaying the div without the href which is the else portion of the code, same goes for when the metabox is empty. I have looked around but have been able to figure out a solution for the if get_post_meta to work with a repeatable group. All of the metabox names are correct and display properly once I remove the if statement.

What am i missing with the if get_post_meta that will correct this issue?

 <?php echo '<section class="row cf">';?>
 <?php global $post; $entries = get_post_meta( get_the_ID(), 'news_group', true ); foreach ( $entries as $key => $entry ) {?>
 <?php if ( get_post_meta($post->ID, 'newblock_url', true) ) : ?>
     <a href="<?php echo $entries [$key]['newblock_url'];?>">
     <article class="col-1 spotlight">
        <?php if ( $entries [$key]['news_image'] ) : ?><img src="<?php echo $entries [$key]['news_image'];?>" alt="<?php echo get_the_title(); ?>"/><?php endif; ?>
        <div class="spotlight-content">
            <h5><?php echo $entries [$key]['news_image_title'];?></h5>
            <p><?php echo $entries [$key]['news_image_desc'];?></p>
        </div>
    </article>
</a>
 <?php endif; ?>
  <article class="col-1 spotlight">
 <?php if ( $entries [$key]['news_image'] ) : ?>     
    <img src="<?php echo $entries [$key]['news_image'];?>" alt="<?php echo get_the_title(); ?>"/><?php endif; ?>
        <div class="spotlight-content">
            <h5><?php echo $entries [$key]['news_image_title'];?></h5>
            <p><?php echo $entries [$key]['news_image_desc'];?></p>
        </div>
    </article>
 <?php } //endforeach ?>
 <?php echo '</section>';?>

closed as off-topic by Mark Kaplun, Sumit, Howdy_McGee, Mayeenul Islam, bueltge Jun 26 '16 at 6:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • you have enough rep here to know that plugin specific questions are off-topic here :( – Mark Kaplun Jun 10 '16 at 16:54
  • this is not a plugin question @MarkKaplun – bigant841 Jun 10 '16 at 16:55
  • either it is a plugin question or a PHP question, the only wordpress specific code is get_post_meta and you use it correctly – Mark Kaplun Jun 10 '16 at 16:57
  • it's a php question @MarkKaplun My issue has nothing to do with CMB2 but with my PHP code – bigant841 Jun 10 '16 at 16:59
  • Which is why I am confused as to why it is not work with a repeatable group @MarkKaplun – bigant841 Jun 10 '16 at 17:03
0

It's unclear from your question what the actual problem is; on a hunch, should it be this:

if ( ! empty( $entries[ $key ]['newblock_url'] ) ) :

... instead of:

if ( get_post_meta( $post->ID, 'newblock_url', true ) ) :

Not the answer you're looking for? Browse other questions tagged or ask your own question.