【问题标题】:Magnific Popup get permalink to post parentMagnific Popup 获得永久链接以发布父级
【发布时间】:2018-12-12 02:29:03
【问题描述】:

我设置了一个自定义帖子类型“女性”。有 56 个帖子,包括肖像和简历。然后,我设置了一个图库页面,该页面从每个帖子中获取缩略图并将其显示在图库中。单击缩略图会使用 Magnific Popup 打开带有全尺寸图像的模式。我陷入困境的地方是试图弄清楚如何将图像标题中的永久链接添加到女性单页?在这个 sn-p 中,永久链接链接回当前页面而不是帖子父级。我可以得到一些格式化 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>';
        }
    }
});

【问题讨论】:

  • 呈现画廊项目 HTML 的 PHP 代码在哪里?
  • 谢谢。我刚刚将 PHP 添加到 OP 中。
  • 我刚刚发布了一个答案。让我知道。 :)

标签: javascript php wordpress custom-post-type magnific-popup


【解决方案1】:

PHP部分

&lt;a&gt; 标签中,将href 设置为单个帖子永久链接,并将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') 获取单个帖子永久链接:

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

我在 CodePen 上放了一个demo,有关data-mfp-src 属性的更多详细信息,请参阅documentation

【讨论】:

  • 太棒了。太感谢了。我在这个问题上花了几个小时,但从未接近你提供的这个解决方案。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
相关资源
最近更新 更多