AJAX调用中的当前POST始终为零

时间:2014-05-26 作者:Nicolae

我有一个名为event的自定义帖子类型和一个加载更多时间线的按钮。我需要根据WP\\U查询对象的当前帖子,将当前时间线项目向左或向右排列。例如:

if (current_post % 2 == 0) {
go to the left
} else {
go to the right
}
对于加载到页面load上的前3个时间线项目,位置是正确的,但每当我调用AJAX时,当前的\\u post是0。以下是我的疑问:

$args = array(
                \'post_type\' => \'timeline\',
                \'order\' => \'ASC\',
                \'posts_per_page\' => (int)$data[\'posts_per_page\'],
                \'offset\' => (int)$data[\'offset\'],
                \'suppress_filters\' => true
            );
        $the_query = new WP_Query($args);
有什么我可以改变的来获得正确的职位吗?

更新为了更清楚,我将把我的全部代码放在这里。AJAX功能:

$(".download-resume").on("click", function() {
        var $this = $(this);
        var posts_per_page = parseInt($this.data("show"));
        var currentPage = parseInt($this.data("current"));
        var offset = currentPage * posts_per_page;

        var data = {
            action : "timeline_load",
            offset : offset,
            posts_per_page : posts_per_page
        };

        var last_item = $(\'.resume-holder:last\');
        $.post(options.ajax_url, data, function(resp) {
            if(resp.success == false) {
                $this.text(resp.data.msg).removeAttr(\'href\');
            } else {
                if(resp.data) {
                    $this.text(resp.data.msg).removeAttr(\'href\');
                } else {
//                    alert(resp);
                    last_item.after(resp);
                    $(\'.resume-slider\').flexslider({
                        animation: "slide",
                        slideDirection: "horizontal",
                        slideshow: false,
                        slideshowSpeed: 3500,
                        animationDuration: 500,
                        directionNav: true,
                        controlNav: false,
                    });
                    $this.data("current", currentPage + 1);
                    $(\'html, body\').animate({
                        scrollTop: $this.offset().top
                    }, 1200);
                }
            }
        });

        return false;
    });
这是我的php函数:

global $post;

        $data = filter_var_array($_POST, FILTER_SANITIZE_SPECIAL_CHARS);

        if ($data[\'action\'] == "timeline_load") {
            $post = null;

            $args = array(
                \'post_type\' => \'timeline\',
                \'order\' => \'ASC\',
                \'posts_per_page\' => (int)$data[\'posts_per_page\'],
                \'offset\' => (int)$data[\'offset\'],
                \'suppress_filters\' => true
            );
            $the_query = new WP_Query($args);

            if ($the_query->have_posts()) :

                while ($the_query->have_posts()) : $the_query->the_post();
                    $icon = get_post_meta($post->ID, \'timeline_icon\', true);
                    $year = get_post_meta($post->ID, \'timeline_year\', true);
//                    var_dump($the_query->current_post);
                    if (($the_query->current_post) % 2 == 0) : ?>
                        <div class="resume-holder">
                            <div class="resume-box animaper">
                                <div class="resume-circle-holder right-circle">
                                    <div class="resume-circle">
                                        <div class="resume-date right-date"><span></span><?php echo $year; ?></div>
                                    </div>
                                </div>
                                <div class="grid-full transition resume left-arrow">
                                    <div class="resume-head right-head-arrow">
                                        <h3><?php the_title(); ?></h3>

                                        <div class="resume-icon"><i class="fa <?php echo $icon; ?>"></i></div>
                                    </div>
                                    <?php
                                    $featured_image = array();
                                    $featured_image[] = get_the_post_thumbnail($post->ID);
                                    if (class_exists(\'Dynamic_Featured_Image\')) {
                                        global $dynamic_featured_image;
                                        $featured_images = $dynamic_featured_image->get_featured_images($post->ID);

                                        if (!is_null($featured_images)) {
                                            foreach ($featured_images as $image) {
                                                $featured_image[] = $image[\'full\'];
                                            }
                                        }
                                    }
                                    $count = count($featured_image);
                                    if ($count > 1) :
                                        ?>
                                        <div class="clear"></div>
                                        <div class="resume-slider">
                                            <ul class="slides">
                                                <?php
                                                $featured_image = get_the_post_thumbnail($post->ID, array(560, 324, \'class\' => \' respimg\'));
                                                echo \'<li>\' . $featured_image . \'</li>\';
                                                if (class_exists(\'Dynamic_Featured_Image\')) {
                                                    global $dynamic_featured_image;
                                                    $featured_images = $dynamic_featured_image->get_featured_images($post->ID);

                                                    if (!is_null($featured_images)) {
                                                        foreach ($featured_images as $image) {
                                                            echo \'<li><img src="\' . $image[\'full\'] . \'" class="respimg" width="560" height="324"></li>\';
                                                        }
                                                    }
                                                }
                                                ?>
                                            </ul>
                                        </div>
                                    <?php else : ?>
                                        <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID, array(560, 325, \'class\' => \' respimg\')), \'single-post-thumbnail\'); ?>
                                        <a class="image-popup" href="<?php echo $image[0]; ?>" title="">
                                            <span></span>
                                            <?php echo get_the_post_thumbnail($post->ID, array(560, 325, \'class\' => \' respimg\')); ?>
                                        </a>
                                    <?php endif; ?>
                                    <?php the_content(); ?>

                                </div>
                            </div>
                        </div>
                    <?php else : ?>
                        <div class="resume-holder">
                            <div class="resume-box right right-box animaper">
                                <div class="resume-circle-holder left-circle">
                                    <div class="resume-circle">
                                        <div class="resume-date left-date"><span></span><?php echo $year; ?></div>
                                    </div>
                                </div>
                                <div class="grid-full transition resume  left-arrow">
                                    <div class="resume-head left-head-arrow">
                                        <h3><?php the_title(); ?></h3>

                                        <div class="resume-icon"><i class="fa <?php echo $icon; ?>"></i></div>
                                    </div>
                                    <?php
                                    $featured_image = array();
                                    $featured_image[] = get_the_post_thumbnail($post->ID);
                                    if (class_exists(\'Dynamic_Featured_Image\')) {
                                        global $dynamic_featured_image;
                                        $featured_images = $dynamic_featured_image->get_featured_images($post->ID);

                                        if (!is_null($featured_images)) {
                                            foreach ($featured_images as $image) {
                                                $featured_image[] = $image[\'full\'];
                                            }
                                        }
                                    }
                                    $count = count($featured_image);
                                    if ($count > 1) :
                                        ?>
                                        <div class="clear"></div>
                                        <div class="resume-slider">
                                            <ul class="slides">
                                                <?php
                                                $featured_image = get_the_post_thumbnail($post->ID, array(541, 313, \'class\' => \' respimg\'));
                                                echo \'<li>\' . $featured_image . \'</li>\';
                                                if (class_exists(\'Dynamic_Featured_Image\')) {
                                                    global $dynamic_featured_image;
                                                    $featured_images = $dynamic_featured_image->get_featured_images($post->ID);

                                                    if (!is_null($featured_images)) {
                                                        foreach ($featured_images as $image) {
                                                            echo \'<li><img src="\' . $image[\'full\'] . \'" class="respimg" width="541" height="313"></li>\';
                                                        }
                                                    }
                                                }
                                                ?>
                                            </ul>
                                        </div>
                                    <?php else : ?>
                                        <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID, array(560, 325, \'class\' => \' respimg\')), \'single-post-thumbnail\'); ?>
                                        <a class="image-popup" href="<?php echo $image[0]; ?>" title="">
                                            <span></span>
                                            <?php echo get_the_post_thumbnail($post->ID, array(541, 313, \'class\' => \' respimg\')); ?>
                                        </a>
                                    <?php endif; ?>

                                    <?php the_content(); ?>

                                </div>
                            </div>
                        </div>
                    <?php endif;
                endwhile;
                wp_reset_query();
            else :
                wp_send_json_success(array("msg" => "No more results to load"));
            endif;
        } else {
            wp_send_json_error(array("msg" => "An error was occured"));
        }

        die();

1 个回复
SO网友:ksr89

用此代码替换ajax代码。我没有检查,但我认为我们应该工作。

整体html 部件被转换为字符串,以便通过ajax响应发送。

<?php
global $post;
$data = filter_var_array($_POST, FILTER_SANITIZE_SPECIAL_CHARS);
if ($data[\'action\'] == "timeline_load") {
    $post = null;

    $args = array(
        \'post_type\' => \'timeline\',
        \'order\' => \'ASC\',
        \'posts_per_page\' => (int)$data[\'posts_per_page\'],
        \'offset\' => (int)$data[\'offset\'],
        \'suppress_filters\' => true
    );
    $the_query = new WP_Query($args);

    if ($the_query->have_posts()) :
        $ajax_response = \'\';
        while ($the_query->have_posts()) : 
            $the_query->the_post();
            $icon = get_post_meta($post->ID, \'timeline_icon\', true);
            $year = get_post_meta($post->ID, \'timeline_year\', true);
            if (($the_query->current_post) % 2 == 0) :
                $ajax_response .= \'<div class="resume-holder">
                    <div class="resume-box animaper">
                        <div class="resume-circle-holder right-circle">
                            <div class="resume-circle">
                                <div class="resume-date right-date"><span></span>\'.$year.\'</div>
                            </div>
                        </div>
                        <div class="grid-full transition resume left-arrow">
                            <div class="resume-head right-head-arrow">
                                <h3>\'.the_title().\'</h3>

                                <div class="resume-icon"><i class="fa \'.$icon.\'"></i></div>
                            </div>\';

                            $featured_image = array();
                            $featured_image[] = get_the_post_thumbnail($post->ID);
                            if (class_exists(\'Dynamic_Featured_Image\')) {
                                global $dynamic_featured_image;
                                $featured_images = $dynamic_featured_image->get_featured_images($post->ID);

                                if (!is_null($featured_images)) {
                                    foreach ($featured_images as $image) {
                                        $featured_image[] = $image[\'full\'];
                                    }
                                }
                            }
                            $count = count($featured_image);
                            if ($count > 1) :
                                $ajax_response .= \'<div class="clear"></div>
                                <div class="resume-slider">
                                    <ul class="slides">\';
                                        $featured_image = get_the_post_thumbnail($post->ID, array(560, 324, \'class\' => \' respimg\'));
                                        $ajax_response .= \'<li>\' . $featured_image . \'</li>\';
                                        if (class_exists(\'Dynamic_Featured_Image\')) {
                                            global $dynamic_featured_image;
                                            $featured_images = $dynamic_featured_image->get_featured_images($post->ID);

                                            if (!is_null($featured_images)) {
                                                foreach ($featured_images as $image) {
                                                    $ajax_response .= \'<li><img src="\' . $image[\'full\'] . \'" class="respimg" width="560" height="324"></li>\';
                                                }
                                            }
                                        }

                                    $ajax_response .= \'</ul>
                                </div>\';
                            else :
                                $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID, array(560, 325, \'class\' => \' respimg\')), \'single-post-thumbnail\');
                                $ajax_response .= \'<a class="image-popup" href="\'.$image[0].\'" title="">
                                    <span></span>\'
                                    .get_the_post_thumbnail($post->ID, array(560, 325, \'class\' => \' respimg\')).
                                \'</a>\';
                            endif;
                            $ajax_response .=the_content().
                        \'</div>
                    </div>
                </div>\';
            else:
                $ajax_response .= \'<div class="resume-holder">
                    <div class="resume-box right right-box animaper">
                        <div class="resume-circle-holder left-circle">
                            <div class="resume-circle">
                                <div class="resume-date left-date"><span></span>\'.$year.\'</div>
                            </div>
                        </div>
                        <div class="grid-full transition resume  left-arrow">
                            <div class="resume-head left-head-arrow">
                                <h3>\'.the_title().\'</h3>
                                <div class="resume-icon"><i class="fa \'.$icon.\'"></i></div>
                            </div>\';
                            $featured_image = array();
                            $featured_image[] = get_the_post_thumbnail($post->ID);
                            if (class_exists(\'Dynamic_Featured_Image\')) {
                                global $dynamic_featured_image;
                                $featured_images = $dynamic_featured_image->get_featured_images($post->ID);

                                if (!is_null($featured_images)) {
                                    foreach ($featured_images as $image) {
                                        $featured_image[] = $image[\'full\'];
                                    }
                                }
                            }
                            $count = count($featured_image);
                            if ($count > 1) :
                                $ajax_response .= \'<div class="clear"></div>
                                <div class="resume-slider">
                                    <ul class="slides">\';
                                        $featured_image = get_the_post_thumbnail($post->ID, array(541, 313, \'class\' => \' respimg\'));
                                        $ajax_response .= \'<li>\' . $featured_image . \'</li>\';
                                        if (class_exists(\'Dynamic_Featured_Image\')) {
                                            global $dynamic_featured_image;
                                            $featured_images = $dynamic_featured_image->get_featured_images($post->ID);

                                            if (!is_null($featured_images)) {
                                                foreach ($featured_images as $image) {
                                                    $ajax_response .= \'<li><img src="\' . $image[\'full\'] . \'" class="respimg" width="541" height="313"></li>\';
                                                }
                                            }
                                        }
                                    $ajax_response .= \'</ul>
                                </div>\';
                            else :
                                $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID, array(560, 325, \'class\' => \' respimg\')), \'single-post-thumbnail\');
                                $ajax_response .= \'<a class="image-popup" href="\'.$image[0].\'" title="">
                                    <span></span>\'.
                                    get_the_post_thumbnail($post->ID, array(541, 313, \'class\' => \' respimg\')).\'
                                </a>\';
                            endif;
                            $ajax_response .= the_content().
                        \'</div>
                    </div>
                </div>\';
            endif;
        endwhile;
        wp_reset_query();
        echo $ajax_response;
        die();
    else :
        echo json_encode(array("msg" => "No more results to load"));
        die();
    endif;
} else {
    echo json_encode(array("msg" => "An error was occured"));
    die();
}

结束

相关推荐

wp_ajax action is not running

我正在写一个插件。当有人按下我的wordpress插件设置页面中的按钮时,我想运行方法get\\u my\\u选项。进行了ajax调用,但该方法从未运行。在页面中,我有以下代码:<div class=\"wrap\"> <input type=\"button\" value=\"test\" onclick=\"my_js_function();\"/> </div> <?php wp_enqueue_script( \'func