对 WordPress 自定义查询进行分页

发布于 2024-09-24 19:30:08 字数 1636 浏览 0 评论 0原文

我如何对此进行分页?这是 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">&lt;&lt;</span> Show More', 'twentyten' ) ); ?></div>
        <div class="nav-next"><?php previous_posts_link( __( 'Show Previous <span class="meta-nav">&gt;&gt;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-10-01 19:30:08

这是一个应该有所帮助的示例:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
   'cat'=>3,
   'caller_get_posts'=>1,
   'post__not_in' => $sticky,
   'paged'=>$paged,
   );
query_posts($args);
?>

query_posts

here's an example that should help:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
   'cat'=>3,
   'caller_get_posts'=>1,
   'post__not_in' => $sticky,
   'paged'=>$paged,
   );
query_posts($args);
?>

query_posts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文