Jump to content

Display text if one of these items is present

Recommended Posts


I have the following code in a WordPress theme which displays a companies website, Twitter and Facebook links.

What I am trying to do is display the text "Useful Links:" if one of the links exists, and hide the text if all are not present.

<p>Useful Links:</p>
<?php if ( $website = get_the_company_website() ) : ?>

                            <a class="company-website" href="<?php echo esc_url( $website ); ?>" itemprop="url" target="_blank" rel="nofollow"><?php _e('Website', 'jobseek'); ?></a>

                        <?php endif; ?>

                        <?php if ( get_the_company_twitter() ) : ?>

                            <a class="company-twitter" href="http://twitter.com/<?php echo get_the_company_twitter(); ?>"  target="_blank"><?php _e('Twitter', 'jobseek'); ?></a>

                        <?php endif; ?>

                        $facebook_link = get_post_meta( get_the_ID(), '_facebook_link', true );
                        if ( ! empty($facebook_link) ) : ?>
                            <a class="company-facebook" href="<?php echo $facebook_link; ?>"  target="_blank">Facebook</a>
   <?php endif; ?>

What would be the best way to show/hide the text depending on if the items are present or not?


Share this post

Link to post
Share on other sites

Perhaps something like

   $links['company_website'] = get_the_company_website();
   $links['company_twitter'] = get_the_company_twitter();
   $links['company_facebook'] = get_post_meta( get_the_ID(), '_facebook_link', true );
   foreach (array_filter($links) as $cls = $link) {
       // output link 


Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.