简体   繁体   English

使用ajax和masonry javascript将wordpress帖子加载到div中,但是href没有加载到div中

[英]Loading wordpress post into div using ajax and masonry javascript, but href isn't loading into div

I've seen a few issues with this before, but within a masonry script I'm not sure if I'm on the right track. 我以前见过这个问题,但在砌砖脚本中,我不确定我是否走在正确的轨道上。

Result: the link opens an entirely new page, when what I'm trying to do is load the content into the div. 结果:链接打开一个全新的页面,当我正在尝试做的是将内容加载到div中。

index.php code: index.php代码:

<div id="primary" class="site-content">
    <div id="content" role="main">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div id="miniLoader"></div>
    <div class="box">
      <a class="miniLoader" href="<?php the_permalink() ?>">
      <?php if (has_post_thumbnail()) {
        the_post_thumbnail('thumbnail');
      }?>
      <h2><?php the_title(); ?></h2></a>
    </div>
    <?php endwhile; endif; ?>
    <?php get_footer(); ?>

header.php code: header.php代码:

 <script>
jQuery(document).ready(function($){
    $('#container').masonry({
      itemSelector: '.box',
      });
});
 </script>

 <?php wp_head(); ?>

<script type="text/javascript"
 src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
 <script type="text/javascript">
$(document).ready(function(){
$.ajaxSetup({cache:false});
    $(".miniLoader").click(function(){
    var post_id = $(this).attr("href")
    $("#miniLoader").html("loading...");
    $("#miniLoader").load(post_link);
    return false;
 });

 });

As far as I know, the placement is in the loop and the script falls into place for where it should be. 据我所知,放置位于循环中,脚本就位于应有的位置。 Or am I getting bad advice for placement and the 'rel'? 还是我得到了关于安置和'rel'的错误建议? I've heard about not using 'rel' anymore what with html5 and wordpress 3.5... 我听说不再使用'rel'了用html5和wordpress 3.5 ...

Any help would be hugely appreciated! 任何帮助将非常感谢!

(please let me know if I'm missing pieces of the puzzle for this to make sense) (请告诉我,如果我错过了这个有意义的拼图)

Try adding event.preventDefault() to disable the normal link action: 尝试添加event.preventDefault()以禁用正常的链接操作:

$(".miniLoader").click(function(event){
    event.preventDefault();

And the post_link variable appears to still need to be set: 并且仍然需要设置post_link变量:

var post_id = $(this).attr("href");
var post_link = 'your-api-file.php?id=' + post_id;
$("#miniLoader").html("loading...");
$("#miniLoader").load(post_link);

Oh and if you don't have an API file, jQuery can actually just load a whole WordPress page, but only display the content from the element that you want. 哦,如果你没有API文件,jQuery实际上只能加载整个WordPress页面,但只显示你想要的元素的内容。 In this case the AJAX load request will only grab and show what's in the #content element: 在这种情况下,AJAX加载请求只会抓取并显示#content元素中的内容:

$('#miniLoader').load('/?p='+ post_id +' #content');

@hansvedo @hansvedo

After much mucking about, here is how I've solved the script problem: 经过多次讨论,以下是我解决脚本问题的方法:

$(document).ready(function(){

$.ajaxSetup({cache:false});
$("h1 a").click(function(event){
    event.preventDefault();
    var post_link = $(this).attr("href");
    $("#miniloader").html("loading...");
    $("#miniloader").load(post_link);
return false;
});

});

and the index alteration as follows: 和指数变更如下:

<div id="miniloader" class="miniloader"></div>

I've been trying to implement your hint at only loading the #content, but to no avail. 我一直试图实现你的提示只加载#content,但无济于事。 Looks like there isn't a 'content' ID, but I did try to implement one in the code below: 看起来没有“内容”ID,但我确实尝试在下面的代码中实现一个:

 <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 <header class="entry-header">
 <h1 class="entry-title"><?php the_title(); ?></h1>
 <div class="entry-meta">
 <?php toolbox_posted_on(); ?>
 </div><!-- .entry-meta -->
 </header><!-- .entry-header -->
 <div class="entry-content" id="content">
 <?php the_content(); ?>
 <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'toolbox' ), 'after' => '</div>' ) ); ?>
 </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM