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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效