2012-08-09 66 views
4

首先,我认为这是一个很长的过程,但希望有人可以帮助。WordPress的 - 我怎样才能通过AJAX获取更多的职位?

为了解释目前的情况,目前我有一个自定义插件,可以获取有关用户及其最近4篇帖子的各种信息。

我也是用的WPBook插件(所以这是在Facebook和仅仅只是一个典型的WordPress站点)

好了,这里是我的代码是抓住了4所最近发表的文章的用户:

// The Query 
query_posts('posts_per_page=4&author='.$blogger_id); 

// set $more to 0 in order to only get the first part of the post 
global $more; 
$more = 0; 

// the Loop 
while (have_posts()) : the_post(); 
?> 
    <div class="oe-grid-box"> 
     <a href="<?php the_permalink() ?>" <?php the_title()?></a> 
     <div class="oe-grid-box-content"> 
      <?php echo the_content('...'); ?> 
     </div> 
     <div class="oe-grid-pic"> 
      <a href="<?php the_permalink() ?>"> 
       <span class="<?php echo strtolower($category); ?>"></span> 
      </a> 
      <?php echo $image; ?> 
     </div> 
    </div> 
<?php 
endwhile; 

// Reset Query 
wp_reset_query(); 
?> 


<div id="load-more">Load More</div> 

我尝试下面这个教程,但不是一个单独的插件,配股代码在我现有的插件,但现在我的网页无法加载:

http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/

理想情况下,我希望当单击加载更多按钮时,获取4个更多帖子并显示它们。

如果有人可以帮助,我会很感激。

UPDATE

好吧,

到目前为止,我在的jQuery已经添加到调用PHP文件,该文件应该有希望恢复职位,但它不工作。

我在想这是因为它不知道WordPress的功能是什么?

如果我在我的加载脚本中加入了一个简单的echo,那么jQuery成功函数会显示一个警告,说明它的工作原理,但是如果我开始做WordPress的东西,我会得到一个内部服务器错误, more.php文件:

// load wordpress into template 
$path = $_SERVER['DOCUMENT_ROOT']; 
define('WP_USE_THEMES', false); 
require($path .'/wp-load.php'); 


$blogger_id = $_GET['id']; 
$firstname = $_GET['firstname']; 
$surname = $_GET['surname']; 
$posts  = $_GET['posts'] + 4; 
$category = $_GET['category']; 
$image  = $_GET['image']; 

// The Query 
query_posts('paged='.get_query_var('paged').'&posts_per_page=' . $posts . '&author='.$blogger_id); 

// set $more to 0 in order to only get the first part of the post 
global $more; 
$more = 0; 

// then the same loop as in my page that is making the ajax call. 
+0

嗯,我会用jQuery和这篇文章来拉正确的帖子:http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html – Jeremy 2012-08-09 16:46:40

+0

当你说临时页不会加载,你是什么意思?它是否显示一些错误消息? – 2012-08-09 16:49:19

+0

@VladimirKadalashvili - 是的,分页符,没有显示错误消息,但可能是因为我没有打开错误报告。 – martincarlin87 2012-08-10 08:08:10

回答

13

经过大量的努力,我找到了答案,这里是一个解决方案,为那些卡在相同的位置。

这正好在你的插件页面,在这里你得到了用户等

<script type="text/javascript"> 
    $(document).ready(function() { 

     posts = 8; 
     author_posts = parseInt(<?php echo $author_posts; ?>); 

     $("#link_selector").click(function() { 

      // alert('posts - ' + posts + 'author posts - ' + author_posts); 


      if ((posts - author_posts) > 3) { 
       $("#link_selector").text('No more posts!'); 
      } 
      else { 

       var category  = '<?php echo strtolower($category); ?>'; 
       var id    = '<?php echo $blogger_id; ?>'; 
       var firstname  = '<?php echo $firstname; ?>'; 
       var surname   = '<?php echo $surname; ?>'; 


       // alert(posts + category + id + firstname + surname); 

       $.ajax({ 
        type: "GET", 
        url: "/wordpress/wp-admin/admin-ajax.php", 
        dataType: 'html', 
        data: ({ action: 'loadMore', id: id, firstname: firstname, surname: surname, posts: posts, category: category}), 
        success: function(data){ 
         $('#ajax_results_container').hide().fadeIn('slow').html(data); 
         posts = posts + 4; 

         if ((posts - author_posts) > 3) { 
          $("#link_selector").text('No more posts!'); 
         } 

        } 
       }); 
      } 

     }); 
    }); 

    </script>  

然后在主题functions.php帖子,把你的功能做你的Ajax请求:

function loadMore() { 

    $blogger_id = $_GET['id']; 
    // get your $_GET variables sorted out 

    // setup your query to get what you want 
    query_posts('posts_per_page=' . $posts . '&author='.$blogger_id); 

    // initialsise your output 
    $output = ''; 

    // the Loop 
    while (have_posts()) : the_post(); 

     // define the structure of your posts markup 

    endwhile; 

    // Reset Query 
    wp_reset_query(); 

    die($output); 

} 

然后,刚过你的函数的关闭,放在行动挂钩

add_action('wp_ajax_loadMore', 'loadMore'); 
add_action('wp_ajax_nopriv_loadMore', 'loadMore'); // not really needed 

这就是它!

+0

我明白,这已经很长时间了..)但是,请说出这个php文件应该包含什么内容?'url:“/wordpress/wp-admin/admin-ajax.php”'我应该如何“定义你的帖子标记的结构”?只需插入HTML没有任何东西?谢谢。 – 2015-06-02 17:44:17

+0

嗨@mankutila,它已经有一段时间了,所以我很难记住。我确信'loadMore()'在'functions.php'(来自你的主题)中,如问题所述,'/ wordpress/wp-admin/admin-ajax.php'必须以某种方式使用它,如果你编辑一个WordPress核心文件,每次更新时都会丢失所有更改。所提到的标记只是你的html,所以通过字符串连接来构建它,例如在循环中:'$ output + ='your html here';' – martincarlin87 2015-06-02 20:57:01

+0

这意味着我应该而不是'定义你的帖子标记的结构'插入'$ output ='

'?谢谢 – 2015-06-02 21:17:46