Php wordpress中的$query\u字符串返回空值?
我正在尝试创建一个分页系统,并使用$query_string变量使其在每个页面上都能工作,该变量应该包含关于类别的所有信息,等等 所以我在做这样的事情:Php wordpress中的$query\u字符串返回空值?,php,jquery,wordpress,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_st
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']);
它在myfunctions.php文件中。我已经全球化了header.php文件中的$query\u string
变量。$\u POST['query\u string']
来自一个javascript函数(也在我的functions.php文件中),我已将该函数设置为wp\u head(因此我假定为文档的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) {
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 →</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();
}
$.post('/wp admin/admin ajax.php'{
行动:'和_行动',
关闭:偏移量+数字,
页码:页码,
查询字符串:“”
},函数(数据){
但是,进一步检查后,它会将query_字符串变量显示为null。
所以当我这么做的时候:
什么都不返回。你知道为什么吗?谢谢:)
更新 这里有最新消息 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>
add_filter('wp_head','javascript_page');
函数javascript_page(){
?>
$(文档).ready(函数(){
var数=10;
var偏移=0;
变量页码=2;
var busy=false;
/*将滚动函数绑定到事件*/
$(窗口).bind('scroll',函数(e){
/*如果滚动高度加上窗口高度大于文档高度减10,请继续*/
if($(窗口).scrollTop()+$(窗口).height()>$(文档).height()-10&&!忙){
忙=真;
/*快速消息,让您知道更多的东西正在加载*/
$('.loading more').html('单击以加载更多文章..');
$.post(“/wp admin/admin ajax.php”{
行动:'和_行动',
关闭:偏移量+数字,
页码:页码,
查询字符串:“”
},函数(数据){
偏移量=偏移量+数量;
$(“.empty div”).append(“欢迎来到页面”+页码+”
+数据);
忙=假;
页码+=1;
});
}
});
$('.loading more').bind('click',函数(e){
忙=真;
$('.loading more').html('正在加载更多文章..'))
/*快速消息,让您知道更多内容正在加载*/
$.post(“/wp admin/admin ajax.php”{
行动:'和_行动',
关闭:偏移量+数字,
页码:页码,
查询字符串:“”
},函数(数据){
偏移量=偏移量+数量;
$(“.empty div”).append(“欢迎来到页面”+页码+”
+数据);
忙=假;
页码+=1;
$('.loading more').html('单击以加载更多文章..');
});
});
});
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) {
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 →</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();
}
add_action('wp_ajax_and_action','get_posts_page');
添加动作(“wp\u ajax\u nopriv\u和”动作“,”获取文章页面“);
函数get_posts_page(){
$query\u string=$\u POST['query\u string'];
全球$wpdb;
查询发布($query\u string.&posts\u per\u page=10&post\u status=publish&offset='。$\u post['off']);
if(have_posts()):while(have_posts()):the_post();
?>
在header.php
文件中全局化$query\u字符串
,但需要在函数javascript\u page()中全局化它
函数javascript\u page(){
全局$query\u字符串;
?>
在那里,我更新了我的答案:)哈哈,我从来没有遇到过比这更有用的人。非常感谢:)祝你愉快;D