ACF Media Credit requires Advanced Custom Fields Pro to be installed & active or the free version of ACF with the ACF Repeater premium add-on installed & active.
';
$i = 1;
$total = count(get_field('media_credit', $attachment_id));
//http://www.advancedcustomfields.com/resources/repeater/
while( have_rows( 'media_credit', $attachment_id ) ) : the_row();
$image_credit .= '';
$credit = get_sub_field('credit');
$credit_link = get_sub_field('credit_link');
// If credit is linked, else just display credit
// Also checks to see if it's the last item in loop
if($total !== '1' && $total !== $i){
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ''.$credit.' | ';
}
else{
$image_credit .= $credit . ' | ';
}
}
else{
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ''.$credit.'';
}
else{
$image_credit .= $credit;
}
}
$image_credit .= '';
$i++;
endwhile;
$image_credit .= '';
endif;
return $image_credit;
}
/**
* Template tag to print the media credit as html.
* @param int|object $attachment_id is the attachment object ID
* @donaldG
*/
function the_media_credit($attachment_id) {
echo the_media_credit_html($attachment_id);
}
/**
* Template tag to print the media credit as html.
* @param int|object $attachment_id is the attachment object ID
* @donaldG
*/
function the_plain_media_credit($attachment_id) {
if ( have_rows('media_credit', $attachment_id ) ) :
$image_credit = '';
$i = 1;
$total = count(get_field('media_credit', $attachment_id));
while( have_rows( 'media_credit', $attachment_id ) ) : the_row();
$image_credit .= '';
$credit = get_sub_field('credit');
$credit_link = get_sub_field('credit_link');
// If credit is linked, else just display credit
// Also checks to see if it's the last item in loop
if($total !== '1' && $total !== $i){
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ''.$credit.' | ';
}
else{
$image_credit .= $credit . ' | ';
}
}
else{
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ''.$credit.'';
}
else{
$image_credit .= $credit;
}
}
$image_credit .= '';
$i++;
endwhile;
$image_credit .= '';
endif;
return $image_credit;
}
/**
* Template tag to print the current post's thumbnail media credit as html.
* @param int|object $post Optional post ID or object of attachment. Default is global $post object.
* @donaldG
*/
if (!function_exists('the_post_thumbnail_media_credit')) {
function the_post_thumbnail_media_credit($post_id=null) {
global $post;
if ( $post_id && $post_id != 0) {
$post_thumbnail_id = get_post_thumbnail_id( $post_id ) ;
}
else{
$post_thumbnail_id = get_post_thumbnail_id( $post->ID ) ;
}
the_media_credit($post_thumbnail_id);
}
}
/**
* Filter the img caption shortcode to add our media credit
* using the get_captioned_credit($attachment_id) function
* http://codex.wordpress.org/Plugin_API/Filter_Reference/img_caption_shortcode
* @donaldG
*/
function filter_img_caption_shortcode( $empty, $attr, $content ){
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
$credit_id = $attr['id'];
$credit_id = str_replace('attachment_', '', $credit_id);
if(current_theme_supports( 'html5') && the_media_credit_html($credit_id) && get_field('media_credit', $credit_id) ){
return ''
. do_shortcode( $content )
. the_media_credit_html($credit_id)
. ''. $attr['caption'] . ''
. '';
}
else if( the_media_credit_html($credit_id) && get_field('media_credit', $credit_id) ){
return '
';
}
}
add_filter( 'img_caption_shortcode', 'filter_img_caption_shortcode', 10, 3 );
/**
* Filters the content for images and returns a wrapping div w/ the media credit following the image
* or image link if it exists.
* @donaldG & Alicia (@bedsheet)
* Thank you Alicia for helping me get started w/ the regex & fixing my loop!
*/
function filter_images($content){
$images = array();
// Find all images with an ID in content
preg_match_all( '/wp-image-[0-9]+/', $content, $images );
if ($images) {
// For each image build the credit and add to content
foreach ($images[0] as $image) {
$get_id = preg_match('/[0-9]+/', $image, $the_id);
$attachment_id = $the_id[0];
//http://www.advancedcustomfields.com/resources/repeater/
if ( have_rows('media_credit', $attachment_id ) ) :
$image_credit = '';
$i = 1;
$total = count(get_field('media_credit', $attachment_id));
while( have_rows( 'media_credit', $attachment_id ) ) : the_row();
$image_credit .= '';
$credit = get_sub_field('credit');
$credit_link = get_sub_field('credit_link');
// If credit is linked, else just display credit
// Also checks to see if it's the last item in loop
if($total !== '1' && $total !== $i){
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ''.$credit.' | ';
}
else{
$image_credit .= '' . $credit . ' | ';
}
}
else{
if (!empty($credit_link) && !empty($credit) ) {
$image_credit .= ''.$credit.'';
} else{
$image_credit .= ''. $credit .'';
}
}
$image_credit .= '';
$i++;
endwhile;
$image_credit .= '';
// Find image with our ID
// This may seem like a lot but we need to account for images wrapped in a
tag or if for some reason autop is turned off
// If things look broken it's b/c both images are in the same paragraph tag, they shouldn't be. So, don't do that!
if(preg_match('/