Magnific Popup 获得 post parent 的永久链接

Magnific Popup get permalink to post parent

我设置了自定义 Post 类型 "Women"。有 56 posts 包括肖像和生物。然后,我设置了一个画廊页面,从每个 post 获取缩略图并将其显示在画廊中。单击缩略图使用 Magnific Popup 打开带有 full-size 图像的模式。我被卡住的地方是试图弄清楚如何从图像标题添加永久链接到女性单页?在此片段中,永久链接链接回当前页面而不是 post parent。我可以得到一些格式化 titleSrc 的指导吗?谢谢。

<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') + ' &bull; <a href="<?php echo get_the_permalink(); ?>">Her Story</a>';
        }
    }
});

PHP部分

<a> 标签中,将 href 设置为单个 post 永久链接,并将 data-mfp-src 设置为图片 URL ($url):

<li>
  <a href="<?php the_permalink(); ?>" data-mfp-src="<?php echo $url; ?>" title="<?php echo $title; ?>">...</a>
</li>

JS部分

titleSrc 回调中,您可以使用 item.el.attr('href') 获取单个 post 永久链接:

titleSrc: function(item){
  return item.el.attr('title') + ' &bull; <a href="' + item.el.attr('href') + '">Her Story</a>';
}

我放一个demo on CodePen, and for more details about the data-mfp-src attribute, refer to the documentation.