我有一个wordpress代码,它在工厂中非常流行。这只是一个显示帖子的循环。它将作为分页系统的一部分工作,我假设执行此操作的唯一方法是运行一个查询,该查询将根据用户是在类别中还是在搜索页面上进行更改,以便用户获得不同的集合的帖子。
if ( have_posts() ) : while ( have_posts() ) : the_post();
// ....
endwhile; endif;
不幸的是我不知道该怎么做。我尝试了各种各样的东西,但没有一个真正有助于这种情况。我似乎总是只关注所有帖子,而不是我正在查看的类别。我假设我应该使用query_posts。 :(
分页是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>
我将点击事件绑定到它也只是为了备份。任何帮助都非常感谢:)
这是PHP im在functions.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();
}
答案 0 :(得分:2)
在类别或搜索页面上使用query_posts
时,您需要确保包含$query_string
变量。 $query_string
变量包含的信息包括您所在的类别以及搜索字词的内容。
<?php query_posts($query_string . '&posts_per_page=10') ?>
好吧,看起来你的javascript是从页面的页脚运行的。因此,您可以将$query_string
变量添加为post参数。
<?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'];
}
您的functions.php
文件应该像这样开始。而且您不必在页脚中全局化$query_string
。您在header.php
中执行此操作的原因是因为header.php正在函数get_header()
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();