I set up a Custom Post Type "Women". There are 56 posts that include a portrait and bio. Then, I set up a Gallery page that gets the thumbnail from each post and displays it in the gallery. Clicking a thumbnail opens a modal with the full-size image using Magnific Popup. Where I get stuck is trying to figure out how to add a permalink from the image caption to the Women single page? In this snippet, the permalink links back to the current page and not the post parent. Can I get some direction formatting the titleSrc? Thank you.
<ul class="popup-gallery scroll-effect">
<?php
$args = array('post_type' => 'women','orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 100);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image = get_field('portrait');
$post_id = get_field('url', false, false);
if( !empty($image) ):
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$size = 'medium';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
$height = $image['sizes'][ $size . '-height' ];
?>
<li>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>">
<img class="icon" src="<?php echo get_template_directory_uri(); ?>/i/icon-image-expand.png">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
</a>
</li>
<?php endif; ?>
<?php endwhile; wp_reset_query();?>
</ul>
$('.popup-gallery').magnificPopup({
delegate: 'a',
type: 'image',
tLoading: 'Loading image #%curr%...',
mainClass: 'mfp-img-mobile',
gallery: {
enabled: true,
navigateByImgClick: true,
preload: [0,1]
},
image: {
tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
titleSrc: function(item) {
return item.el.attr('title') + ' • <a href="<?php echo get_the_permalink(); ?>">Her Story</a>';
}
}
});
In the <a>
tag, set the href
to the single post permalink, and set data-mfp-src
to the image URL ($url
):
<li>
<a href="<?php the_permalink(); ?>" data-mfp-src="<?php echo $url; ?>" title="<?php echo $title; ?>">...</a>
</li>
In the titleSrc
callback, you can then use item.el.attr('href')
to get the single post permalink:
titleSrc: function(item){
return item.el.attr('title') + ' • <a href="' + item.el.attr('href') + '">Her Story</a>';
}
I put a demo on CodePen, and for more details about the data-mfp-src
attribute, refer to the documentation.