Luis Izquierdo
Luis Izquierdo

Reputation: 41

Hiding empty values and Linking values

I am using a WordPress site and wanted to integrate photo captions and photo credit line under the fatured image. I found a great code that adds a Photographer credit and URL field to the media upload page. However, upon displaying it in the page, I want to further customize it.

Currently, when i upload a photo, i can add a photographer name, and a photographer (or source) URL. The URL automatically links to the name if filled in. However, what I want to do is add a text and icon before the photographer name to say "(icon) Image Credit / "

I was able to add it using css, however, when there is no photographer name, it still shows the Image Credit text and icon but no name. How can I hide that when there is no value in the photographer name field?

also, if i enter a photographer name, but no URL, it links the name to an empty URL. How can i make it so that it does not link the name if the URL field is empty?

Here is the code i am working with:

function be_attachment_field_credit( $form_fields, $post ) {
$form_fields['be-photographer-name'] = array(
    'label' => 'Photographer Name',
    'input' => 'text',
    'value' => get_post_meta( $post->ID, 'be_photographer_name', true ),
    'helps' => 'If provided, photo credit will be displayed',
);

$form_fields['be-photographer-url'] = array(
    'label' => 'Photographer URL',
    'input' => 'text',
    'value' => get_post_meta( $post->ID, 'be_photographer_url', true ),
    'helps' => 'Add Photographer URL',
);

return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );

/**
 * Save values of Photographer Name and URL in media uploader
 *
 * @param $post array, the post data for database
 * @param $attachment array, attachment fields from $_POST form
 * @return $post array, modified post data
 */

function be_attachment_field_credit_save( $post, $attachment ) {
    if( isset( $attachment['be-photographer-name'] ) )
        update_post_meta( $post['ID'], 'be_photographer_name', $attachment['be-photographer-name'] );

    if( isset( $attachment['be-photographer-url'] ) )
update_post_meta( $post['ID'], 'be_photographer_url', esc_url( $attachment['be-photographer-url'] ) );

    return $post;
}

add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );

Then in my single.php file I added the following code to display the photo credit.

<div id="tgg_credit_line" class="tgg-photo-credit" align="right">
  <?php echo "&#x1F4F7; Image Credit / " ?><a href="<?php echo get_post_meta(get_post_thumbnail_id(), 'be_photographer_url', true); ?>"><?php echo get_post_meta(get_post_thumbnail_id(), 'be_photographer_name', true); ?></a>
  </div>

Upvotes: 0

Views: 66

Answers (1)

JasonB
JasonB

Reputation: 6368

You can first check to see if there is a photographer name set, and if not, don't add any of the credit code. You can also check whether there is a url and then only add the link tag when necessary. This code should be used in place of what you have in the single templates.

<?php 
$photographer_name = get_post_meta(get_post_thumbnail_id(), 'be_photographer_name', true);
$photographer_url = get_post_meta(get_post_thumbnail_id(), 'be_photographer_url', true);

if ( $photographer_name ) : ?>
    <div id="tgg_credit_line" class="tgg-photo-credit" align="right">
    &#x1F4F7; Image Credit /
    <?php if ( $photographer_url ) : ?> 
         <a href="<?php echo $photographer_url ?>">
    <?php endif; ?>
    <?php echo $photographer_name ?>
    <?php if ( $photographer_url ) : ?> 
         </a>
    <?php endif; ?>
    </div>
<?php endif; ?>

Upvotes: 1

Related Questions