您好我需要为wordpress网站创建一个简单的弹出功能。
我正在运行循环并正确显示帖子。点击后的帖子应该出现在弹出窗口中。到目前为止我得到了什么。除了添加fancybox来完成它的工作。
<a class="modalbox" rel="<?php echo $post->ID; ?>" href=" http://localhost/makijaz/?page_id=12">
<article> ...Wordpress post </article>
我从其他线程中得到了一个,但它不起作用。
$(".modalbox").on("click", function() {
var postId = $(this).prop("rel");
$(this).fancybox();
});
href in指向带有其他循环的模板的页面。需要简单地克写PostID(它是在一个相关的内容中)并将其放入其他循环中以便在弹出窗口中显示。
<?php
/*
Template Name: Ajax Post Handler
*/
?>
<?php
$post = get_post($_GET['id']);
?>
<?php if ($post) : ?>
<?php setup_postdata($post); ?>
<div class="whatever">
<h2 class="entry-title"><?php the_title() ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
<?php endif; ?>
希望我已经说清楚了。
答案 0 :(得分:0)
我猜你的页面模板没有get_header和get_footer所以在你的示例脚本中不会加载。
<?php
/*
Template Name: Your Temp Name
*/
get_header(); ?>
答案 1 :(得分:0)
如果您想传递 帖子ID ,以便$post = get_post($_GET['id']);
可以抓取它,您可以尝试
jQuery(document).ready(function ($) {
$(".modalbox").on("click", function (e) {
e.preventDefault();
var postId = $(this).prop("rel");
$.fancybox.open({
href: this.href + "&id=" + postId,
type: "ajax"
});
});
});
参见 JSFIDDLE