【发布时间】:2021-02-01 07:19:41
【问题描述】:
我正在尝试在我的主题中加载 magnific-popup,但它不起作用,我不知道为什么。 当我单击图像时,它只显示标准原始图像。
当我只用 html 构建它时,它运行良好,但是在 wordpress 中它似乎不起作用。
我确实使用 alert('hi'); 测试了 main.js 和 jquery.magnific-popup.min.js;并且他们都吐出了警报,所以他们都在工作。
我的队列文件似乎正确,但我不确定我使用的是正确的 'the_post_thumbnail_url();'在我的页面模板中用于图像链接的功能?
谁能发现问题?
仅供参考:我不想使用该插件。
我的目录图片
排队文件
function add_theme_scripts()
{
wp_enqueue_style('magnific-popup', get_stylesheet_uri() . '/assets/css/magnific-popup/magnific-popup.css', true);
wp_enqueue_style('style', get_stylesheet_uri());
wp_enqueue_script('jquery-3.5.1', get_template_directory_uri() . '/assets/js/jquery-3.5.1.min.js', '3.5.1', true); /*when uploaded to server, wordpress already has jquery*/
wp_enqueue_script('magnific-popup', get_template_directory_uri() . '/assets/js/magnific-popup/jquery.magnific-popup.min.js', '1.1.0', true);
wp_enqueue_script('main', get_template_directory_uri() . '/assets/js/main.js', true);
}
add_action('wp_enqueue_scripts', 'add_theme_scripts');
我的页面-project.php
<?php
/**
* Template Name: Project Page
* Template Post Type: post
*/
get_header();
?>
<div class="content">
<h1 class="title main-title"><?php the_title(); ?></h2>
<?php if (have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile;
endif; ?>
<?php
$projects = new WP_Query(array(
'posts_per_page' => 5,
'post_type' => 'projects',
)); ?>
<div class="the-content">
<?php if ($projects->have_posts()) : while ($projects->have_posts()) : $projects->the_post(); ?>
<div class="post-content">
<div class="project-images">
<?php if (has_post_thumbnail()) : ?>
<a class="popup" href="<?php the_post_thumbnail_url(); ?>">
<img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
</a>
<?php endif; ?>
</div>
<h2 class="title"><?php the_title(); ?></h2>
<?php endwhile;
else : _e('Sorry, no posts seem to have been found');
endif; ?>
</div>
</div>
<div class="step-back">
<a href="<?php echo site_url('/'); ?>">home</a></p>
</div>
来自 main.js 的 js 文件
// magnific popup
jQuery('.project-images').magnificPopup({
delegate: 'a',
type: 'image',
gallery: {
enabled: true,
navigateByImgClick: true,
preload: [0, 3], // Will preload 0 - before current, and 1 after the current image
tPrev: 'Previous', // title for left button
tNext: 'Next', // title for right button
},
removalDelay: 300,
closeOnContentClick: true,
midClick: true,
mainClass: 'mfp-fade',
callbacks: {
buildControls: function () {
// re-appends controls inside the main container
this.contentContainer.append(this.arrowLeft.add(this.arrowRight));
}
}
});
【问题讨论】:
-
你的入队脚本你不会把位置放到你想要的脚本作为依赖,而是脚本的句柄。因此,既然您要添加
jquery-3.5.1,那应该是您的依赖项。array('jquery-3.5.1') -
这对我不起作用,它使网站预加载器无限循环!
-
这并不能解决我的问题...
-
我注意到你的 while 循环在关闭内容的 div 之前结束 - 在 h2 之后将它放在你的循环中
-
如果控制台中显示任何错误怎么办
标签: javascript wordpress magnific-popup