duanba7653 2018-01-31 05:04
浏览 40
已采纳

隐藏空值和链接值

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>
  • 写回答

1条回答 默认 最新

  • duankange2433 2018-01-31 05:33
    关注

    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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值