对 WordPress 自定义查询进行分页
我如何对此进行分页?这是 WordPress 模板中的代码,我用它来按首字母获取帖子。我一页只想要 10 个。
$postids=$wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));
if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts Titles beginning with the letter '. $first_char;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
?>
我知道我们可以控制不。但如何分页呢? 通常在 WordPress 模板内的普通循环中,它的工作方式如下
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-above" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav"><<</span> Show More', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Show Previous <span class="meta-nav">>></span>', 'twentyten' ) ); ?></div>
</div><!-- #nav-above -->
<?php endif; ?>
,但在这种情况下它不起作用。
How do i paginate this? It's a code in wordpress template I'm using to get posts by their first letter. I only want 10 in a page.
$postids=$wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));
if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts Titles beginning with the letter '. $first_char;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
?>
I know that we can control the no. of posts.But how to paginate?
Usually in ordinary loops within wordpress template it works like this
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-above" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav"><<</span> Show More', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Show Previous <span class="meta-nav">>></span>', 'twentyten' ) ); ?></div>
</div><!-- #nav-above -->
<?php endif; ?>
But it doest work in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个应该有所帮助的示例:
query_posts
here's an example that should help:
query_posts