0

I am using custom post/field types in wordpress and some of the fields are social media links.

What I am trying to do is hide the social media icon if the field is left blank in the back end of wordpress, the code below only shows/hides ALL of the icons, i need them to act independently.

<?php $socialmedia = get_post_meta( $post->ID, 'socialmedia', true );
                                                if ($socialmedia) {
                                                    foreach( $socialmedia as $socialmedia ) { ?>
                                                        <a href="http://<?php echo $socialmedia['facebook'] ?>"><img src="<?php bloginfo('template_url'); ?>/img/facebook.png" alt="facebook" width="55" height="56" /></a>
                                                        <a href="http://<?php echo $socialmedia['twitter'] ?>"><img src="<?php bloginfo('template_url'); ?>/img/twitter.png" alt="twitter" width="55" height="56" /></a>
                                                        <a href="http://<?php echo $socialmedia['soundcloud'] ?>"><img src="<?php bloginfo('template_url'); ?>/img/soundcloud.png" alt="soundcloud" width="55" height="56" /></a>
                                        <?php } // end foreach
                                        } // end if ?>

1 Answer 1

1

You can add if condition inside of foreach loop -

if ($socialmedia) {
    foreach( $socialmedia as $media ) {
        if($media['facebook'] != NULL) { ?> // same for other icons
            <a href="http://<?php echo $media['facebook'] ?>"><img src="<?php bloginfo('template_url'); ?>/img/facebook.png" alt="facebook" width="55" height="56" /></a>
        <?php } ?>
<?php } // end foreach
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.