普通网友 2014-11-17 19:06
浏览 84
已采纳

Wordpress静态首页搜索分页

I have a static frontpage on my wordpress website, displaying my posts. Currently it shows 10 posts and I want to make it possible to click "next page" to see the next 10 posts in the query and so on. I have tried getting the pagination to work, but it looks like I'm unable to connect it with my search query.

Here is my query:

<?php 
query_posts(
array(  'post_type' => 'post',
        'order'     => 'DESC',
        'meta_key' => 'cf_votes',
        'orderby'   => 'meta_value_num',
        'posts_per_page' => 10

)
);
?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>

And here is the pagination I have tried:

<?php
global $wp_query;

$big = 999999999;

echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>

So basically I'm able to see the first 10 posts perfectly. But no alternative to see more posts is shown.

  • 写回答

1条回答 默认 最新

  • dsfdgdsfd23212 2014-11-18 14:34
    关注

    Wordpress Codex is pretty comprehensive on the Pagination problem, I recommend you to see this:

    http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

    There's a section specifically for the Static Front Page.

    Something like:

    <?php 
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
    
    $args = array(
      'posts_per_page' => 3,
      'paged' => $paged
    );
    
    query_posts($args); 
    
    ?>
    
    <?php if ( have_posts() ) : ?>
    
    <!-- Add the pagination functions here. -->
    
    <!-- Start of the main loop. -->
    <?php while ( have_posts() ) : the_post();  ?>
    
    <!-- the rest of your theme's main loop -->
    
    <?php endwhile; ?>
    <!-- End of the main loop -->
    
    <!-- Add the pagination functions here. -->
    
    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    
    <?php else : ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题