dongtang6718 2014-04-03 16:11
浏览 75
已采纳

Wordpress分页自定义帖子类型

I have a page called News (using page template page-newslist.php), which is supposed to display posts from the custom post type also named News. I realize that having both with the same name causes issues, so when registering the custom post type, I have a rewrite to differentiate it from the page:

'rewrite' => array('slug' => 'news-article', 'with_front' => true),

I can get the query working and displaying the posts properly, but after all of the articles and posts I've read, I cannot get the pagination to work. Nothing ever shows up for the pagination.

After no success with using the page template query, I tried the archive-news.php method, where it would automatically display the posts from the custom post type. The pagination does work there. The downside of using this method is that there isn't a 'physical' page to tie it to (which would also have custom fields, ability to be nicely added (not hard-coded) into menus, etc.)

Here is the stripped-down code registering the custom post type:

register_post_type('news', array(
    'label' => 'News',
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => array('slug' => 'news-article', 'with_front' => true),
    'query_var' => true,
    'has_archive' => true,
));

And then the code for the page template:

$paged = 1;  
if ( get_query_var('paged') ) $paged = get_query_var('paged');  
if ( get_query_var('page') ) $paged = get_query_var('page');
$args = array(
    'post_type' => 'news',
    'post_status' => 'publish',
    'posts_per_page' => 1,
    'paged' => $paged
);

$my_query = null;
$my_query = new WP_Query($args);

if($my_query->have_posts()):

while ($my_query->have_posts()) : $my_query->the_post();

...

endwhile;

endif;

wp_reset_query();

// Attempt method 1
posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »'));

// Attempt method 2
previous_posts_link('« Newer');
next_posts_link('Older »');

Any ideas what's wrong?

  • 写回答

2条回答 默认 最新

  • dsdqpdjpq16640651 2014-04-03 16:28
    关注

    Have a look at next page link page, the example here will help. codex.wordpress.org/Template_Tags/next_posts_link

    <?php next_posts_link('Older Entries »', 0); ?>
    

    Wordpress codex example.

            <?php
        // set the "paged" parameter (use 'page' if the query is on a static front page)
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
        // the query
        $the_query = new WP_Query( 'cat=1&paged=' . $paged ); 
        ?>
    
        <?php if ( $the_query->have_posts() ) : ?>
    
    
    
      <?php
        // the loop
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
        ?>
        <?php the_title(); ?>
        <?php endwhile; ?>
    
        <?php
    
        // next_posts_link() usage with max_num_pages
        next_posts_link( 'Older Entries', $the_query->max_num_pages );
        previous_posts_link( 'Newer Entries' );
        ?>
    
        <?php 
        // clean up after our query
        wp_reset_postdata(); 
        ?>
    
        <?php else:  ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛