[英]Magnific-popup not working in my WordPress theme page template
我正在嘗試在我的主題中加載 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 沖突。 事實證明我需要使用不同的 jQuery 版本。 這對我有用。
添加 magnific-popup.min.css 后,我不得不注銷現有的 jQuery 並將 3.5.1 版入隊。 請注意,依賴項是 jquery。 這是文檔參考。
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', get_template_directory_uri() . '/js/jquery3.js');
wp_enqueue_script( 'jquery', get_template_directory_uri() . '/js/jquery3.js');
wp_enqueue_script( 'magnific-popup', get_template_directory_uri() . '/js/jquery.magnific-popup.min.js', array('jquery'), '', true );
wp_enqueue_script( 'magnific-popup-init', get_template_directory_uri() . '/js/jquery.magnific-popup.init.js', array('jquery'), '', true );
wp_enqueue_script( 'custom', get_template_directory_uri() . '/js/custom.js', array('jquery'), '', true );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.