wordpress中的$ query_string返回null?

时间:2010-11-16 17:36:51

标签: php jquery wordpress

我正在尝试创建一个分页系统,并使其在每个页面都有效,我正在使用$ query_string变量,该变量可能包含有关该类别的所有信息等。

所以我正在做这样的事情:

add_action('wp_ajax_and_action', 'get_posts_page');
add_action('wp_ajax_nopriv_and_action', 'get_posts_page');

function get_posts_page() {

$query_string = $_POST['query_string'];

global $wpdb;   


query_posts($query_string . '&posts_per_page=10&post_status=publish&offset='.$_POST['off']);    

它在我的functions.php文件中。我已经在header.php文件中全局化了$query_string变量。 $_POST['query_string']来自javascript函数(也在我的functions.php文件中),我将其设置为wp_head(因此我假设文档的头部)。它将大量数据发布到PHP函数:

$.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
action: 'and_action',
off: offset+number,
pagenumber: page_number,
query_string: '<?php echo $query_string; ?>'
}, function(data) {

但是,经过进一步检查,它将query_string变量显示为null。 所以,当我这样做时:<?php echo $query_string ?>没有任何回报。任何想法为什么会这样?谢谢:))


更新

继续更新

的functions.php

Javascript:

   add_filter('wp_head', 'javascript_page');

function javascript_page() {
?>

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

        var number = 10;
        var offset = 0;
        var page_number = 2;
        var busy = false;

        /* Bind the scroll function to an event */
        $(window).bind('scroll', function(e) {


            /* If the scroll height plus the window height is more than the document height minus 10, continue */
            if($(window).scrollTop() + $(window).height() > $(document).height() - 10 && !busy) {


                busy = true;    

                /* Quick message so you know more stuff is loading */
                $('.loading-more').html('Click to load more posts..');

                $.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
                    action: 'and_action',
                    off: offset+number,
                    pagenumber: page_number,
                    query_string: '<?php echo $query_string; ?>'
                    }, function(data) {


                        offset = offset+number; 

                        $('.empty-div').append('<div class="pages"><p>Welcome to <strong>Page '+page_number+'</strong></p></div><hr />'+data);


                        busy = false;
                        page_number += 1;


                });


            }

        });


        $('.loading-more').bind('click', function(e) {

                busy = true;    

                $('.loading-more').html('<em>Loading more posts..</em>')

                /* Quick message so you know more stuff is loading */               
                $.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
                    action: 'and_action',
                    off: offset+number,
                    pagenumber: page_number,
                    query_string: '<?php echo $query_string; ?>'
                    }, function(data) {


                        offset = offset+number; 

                        $('.empty-div').append('<div class="pages"><p>Welcome to <strong>Page '+page_number+'</strong></p></div><hr />'+data);


                        busy = false;
                        page_number += 1;

                        $('.loading-more').html('Click to load more posts..');

                });


        });


    });
    </script>

PHP函数:

add_action('wp_ajax_and_action', 'get_posts_page');
add_action('wp_ajax_nopriv_and_action', 'get_posts_page');

function get_posts_page() {

$query_string = $_POST['query_string'];

global $wpdb;   


query_posts($query_string . '&posts_per_page=10&post_status=publish&offset='.$_POST['off']);    

if ( have_posts() ) : while ( have_posts() ) : the_post(); 


    ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
        <div class="entry-meta">

            <span class="%1$s">Posted on</span> <?php the_date('F jS'); ?>
            - <a class="comment-link" href="<?php the_permalink(); ?>#comment"><?php comments_number('Leave a Response!', '1 Response', '% Responses'); ?></a>
        </div><!-- .entry-meta -->
        <br />
        <a class="post-thumbnail-thing" href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail(); ?></a>            
        <div class="entry-content">
            <?php the_content( __( '<span class="alignright">
            <span class="button-css">Continue Reading &rarr;</span> 
             </span>', 'twentyten' ) ); ?><br /><hr />
            <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
        </div><!-- .entry-content -->
    </div><!-- #post-## -->

    <?php comments_template( '', true ); ?>

   <?php        
   endwhile; endif;
    die();

    }

的header.php

<?php global $query_string; ?>

正如我所说,主要问题是$ query_string为null :(

1 个答案:

答案 0 :(得分:1)

您在$query_string文件中的全球化header.php,但您需要在javascript_page()

内的函数functions.php内进行全球化

Here's a video explaining what I'm doing

function javascript_page() {
  global $query_string;
?>

<script type="text/javascript">