字preSS - 我怎么可以卖到通过AJAX的讯息吗? [英] Wordpress - how can I fetch more posts via AJAX?

查看:161
本文介绍了字preSS - 我怎么可以卖到通过AJAX的讯息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我认为这是一个远射,但希望有一个人在那里,可以提供帮助。

要解释目前的情况来看,目前我有一个自定义插件,抓住各种信息比特有关用户和他们的4最近的帖子。

我还使用了WPBook插件(所以这是Facebook和刚刚只是一个典型字preSS网站)

好了,这是我的code被抓的4个最新的职位用户:

  //该查询
query_posts('posts_per_page = 4和作者='$ blogger_id);

//设置$以上为0,以只获得该职位的第一部分
 全球$以上;
 $更= 0;

 //河套
 而(have_posts()):the_post();
    ?>
    < D​​IV CLASS =OE-格箱>
        < A HREF =< PHP the_permalink()>? &所述; PHP的the_title()>?&所述; / a取代;
        < D​​IV CLASS =OE-网箱内容>
                <?PHP的回声the_content('...'); ?>< / DIV>
        < D​​IV CLASS =OE-格PIC>
            < A HREF =< PHP the_permalink()>>
                <跨度类=< PHP的回声用strtolower($类);?>>< / SPAN>
            &所述; / a取代;
            <?PHP的echo $图像; ?>
        < / DIV>
    < / DIV>
< PHP
ENDWHILE;

//重置查询
wp_reset_query();
?>


< D​​IV ID =加载更多>负载详情< / DIV>
 

我尝试下面这个教程,但不是一个单独的插件,将在code。在我现有的插件,但现在我的网页将不会加载:

HTTP:/ /www.problogdesign.com/word$p$pss/load-next-word$p$pss-posts-with-ajax/

在理想情况下,要我想是被点击加载更多的按钮时,取4个职位,并告诉他们。

如果有人可以帮助我真的AP preciate吧。

更新

好了,

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

我在想,这是因为它不知道这个词preSS功能是什么呢?

如果我把一个简单的回声在我的装载更多的脚本,jQuery的成功,函数显示警报说,它的工作,但如果我开始做文字preSS的东西,我得到一个内部服务器错误,这是code在负载more.php文件:

  //负荷字preSS成模板
$ PATH = $ _ SERVER ['DOCUMENT_ROOT'];
定义(WP_USE_THEMES',假);
需要($路径/ WP-load.php');


$ blogger_id = $ _GET ['身份证'];
$名字= $ _GET ['姓'];
$姓= $ _GET ['姓'];
$帖= $ _GET ['帖'] + 4;
$类别= $ _GET ['类'];
$图像= $ _GET ['形象'];

//该查询
query_posts('分页='get_query_var(分页)。'和; posts_per_page ='$职位。'和,作家='$ blogger_id。);

//设置$以上为0,以只获得该职位的第一部分
全球$以上;
$更= 0;

//那么同样的循环在我的页面,该页面正在Ajax调用。
 

解决方案

很多的努力后,我找到了答案,在这里是为那些停留在同一位置的解决方案。

这正好在你的插件页面,你得到的职位,用户等。

 <脚本类型=文/ JavaScript的>
    $(文件)。就绪(函数(){

        帖子= 8;
        author_posts = parseInt函数(小于?PHP的echo $ author_posts;?>);

        $(#link_selector)。点击(函数(){

            //警报('的帖子 - +岗位+作者的帖子 - + author_posts);


            如果((职位 -  author_posts)> 3){
                $(#link_selector)文本。(没有更多内容!');
            }
            其他 {

                VAR类='<?PHP的回声用strtolower($类); ?>';
                VAR ID ='<?PHP的echo $ blogger_id; ?>';
                VAR名字='<?PHP的echo $名字; ?>';
                VAR姓='<?PHP的echo $姓氏; ?>';


                //警报(职务+分类+编号+姓名+姓);

                $阿贾克斯({
                    键入:GET,
                    网址:/word$p$pss/wp-admin/admin-ajax.php
                    数据类型:HTML,
                    数据:({行动:loadMore,ID:ID,姓:姓,姓:姓,职位:职位,类别:类别}),
                    成功:功能(数据){
                        。$('#ajax_results_container)隐藏()淡入(慢)HTML(数据);
                        帖子=职位+ 4;

                        如果((职位 -  author_posts)> 3){
                            $(#link_selector)文本。(没有更多内容!');
                        }

                    }
                });
            }

        });
    });

    < / SCRIPT>
 

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

 函数loadMore(){

    $ blogger_id = $ _GET ['身份证'];
    //得到你的$ _ GET变量整理

    //设置查询得到你想要的东西
    query_posts('posts_per_page ='$帖'和,作家='。$ blogger_id);

    // initialsise你的输出
    $输出='';

    //河套
    而(have_posts()):the_post();

        //定义你的帖子的标记结构

    ENDWHILE;

    //重置查询
    wp_reset_query();

    死亡($输出);

}
 

然后,只需你的函数的关闭,放在行动挂钩后

  add_action('wp_ajax_loadMore','loadMore');
add_action('wp_ajax_nopriv_loadMore','loadMore'); //不是真的需要
 

这就是它!

To begin with, I think this is a longshot but hopefully there is someone out there that can help.

To explain the current situation, at the moment I have a custom plugin that grabs various bits of information about a user and their 4 most recent posts.

I am also using the WPBook plugin (so this is on facebook and just just a typical wordpress site)

Ok, so here is my code that is grabbing the 4 most recent posts for a user:

// 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>

I tried following this tutorial but instead of a separate plugin, placing the code in my existing plugin but now my page won't load:

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

Ideally, want I want to is when the load more button is clicked, fetch 4 more posts and show them.

If anybody could help I'd really appreciate it.

UPDATE

Ok,

so far I've added in my jQuery to call the php file which should hopefully return posts but it doesn't work.

I am thinking that it's because it doesn't know what the WordPress functions are?

If I put a simple echo in my load more script, the jQuery success function shows an alert to say it worked, but if I start doing WordPress stuff I get an internal server error, this is the code in the load-more.php file:

// 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.

解决方案

After a lot of effort, I found the answer, here is a solution for those stuck in the same position.

This goes in your plugin page where you are getting posts for a user etc.

<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>       

Then in your theme's functions.php, put your function to do your ajax request:

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);

}

Then, just after the closing of your function, put in the action hooks

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

That's it!

这篇关于字preSS - 我怎么可以卖到通过AJAX的讯息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆