Php 查询不同页面的wordpress?
我有一个wordpress代码,它在工厂里运行得很好。这只是一个显示帖子的循环。它将作为分页系统的一部分工作,我认为唯一的方法是运行一个查询,该查询将根据用户是在类别中还是在搜索页面上而变化,以便用户获得不同的帖子集Php 查询不同页面的wordpress?,php,jquery,wordpress,Php,Jquery,Wordpress,我有一个wordpress代码,它在工厂里运行得很好。这只是一个显示帖子的循环。它将作为分页系统的一部分工作,我认为唯一的方法是运行一个查询,该查询将根据用户是在类别中还是在搜索页面上而变化,以便用户获得不同的帖子集 if ( have_posts() ) : while ( have_posts() ) : the_post(); // .... endwhile; endif; 不幸的是,我不知道怎么做。我尝试过各种各样的方法,但都没有真正起到帮助作用。我似乎总是得到所有的帖
if ( have_posts() ) : while ( have_posts() ) : the_post();
// ....
endwhile; endif;
不幸的是,我不知道怎么做。我尝试过各种各样的方法,但都没有真正起到帮助作用。我似乎总是得到所有的帖子,而不是我正在查看的类别的帖子。我想我应该使用查询帖子:(
分页是AJAX,它向wordpress循环文件发布两个内容,即偏移量和页码。它基本上检查用户何时向下滚动到页面底部并加载更多内容。此处供参考:
<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 - 1
}, 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,
data: data,
pagenumber: page_number - 1
}, 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>
$(文档).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”{
行动:'和_行动',
关闭:偏移量+数字,
页码:页码-1
},函数(数据){
偏移量=偏移量+数量;
$(“.empty div”).append(“欢迎来到页面”+页码+”
+数据);
忙=假;
页码+=1;
});
}
});
$('.loading more').bind('click',函数(e){
忙=真;
$('.loading more').html('正在加载更多文章..'))
/*快速消息,让您知道更多内容正在加载*/
$.post(“/wp admin/admin ajax.php”{
行动:'和_行动',
关闭:偏移量+数字,
数据:数据,
页码:页码-1
},函数(数据){
偏移量=偏移量+数量;
$(“.empty div”).append(“欢迎来到页面”+页码+”
+数据);
忙=假;
页码+=1;
$('.loading more').html('单击以加载更多文章..');
});
});
});
我也为它绑定了一个点击事件,只是为了备份。非常感谢您的帮助:)
更新 下面是我在functions.PHP中使用的PHP
add_action('wp_ajax_and_action', 'get_posts_page');
add_action('wp_ajax_nopriv_and_action', 'get_posts_page');
function and_action() {
$query_string = $_POST['query_string'];
}
function get_posts_page() {
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;
wp_reset_query();
die();
}
add_action('wp_ajax_and_action','get_posts_page');
添加动作(“wp\u ajax\u nopriv\u和”动作“,”获取文章页面“);
函数和_动作(){
$query\u string=$\u POST['query\u string'];
}
函数get_posts_page(){
全球$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();
?>
在类别或搜索页面上使用query\u posts
时,您需要确保包含$query\u string
变量。$query\u string
变量包含以下信息,如您所在的类别和搜索词
<?php query_posts($query_string . '&posts_per_page=10') ?>
functions.php
更新#2
你的functions.php
文件应该这样开始。你不必在页脚中全球化$query\u string
。你在header.php中这样做的原因是header.php在函数get\u header()中运行
我已经试过了,但是仍然没有骰子。我在functions.php文件中的一个函数中,我需要添加操作还是什么?:/仍然没有!:(javascript在头部,我使用add_过滤器('wp_head','javascript_page'));另外,我已经添加了您说要添加的所有内容。我可以用我正在使用的PHP更新我的答案,等等。哦,我很抱歉不清楚。您需要将我的函数和_操作中的内容放入函数get_posts_页面。这样,您的函数就可以访问变量$query_字符串。好的,我检查了查询字符串。我将其设置为global在header.php文件和footer.php文件(分别)中,它不返回任何内容,就像在null中一样。知道为什么吗?没有。查询字符串仍然为null:(很抱歉,如果我是fbothersome,我很想把这个问题弄清楚。我认为主要的问题是,查询字符串在jQuery中没有返回任何内容。因此,在我有“”的地方,当我查看源代码时,它只显示为“”。
<?php global $query_string; ?>
...
$.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
action: 'and_action',
off: offset+number,
pagenumber: page_number - 1,
query_string: '<?php echo $query_string ?>'
}
function and_action() {
$query_string = $_POST['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']);
if ( have_posts() ) : while ( have_posts() ) : the_post();