WordPress分页自定义文章类型

3

我有一个名为“新闻”的页面(使用页面模板 page-newslist.php ),应该显示来自也名为“新闻”的自定义文章类型的文章。我意识到拥有相同名称会导致问题,因此在注册自定义文章类型时,我进行了重写以将其与页面区分开:

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

我可以让查询工作并正确显示文章,但是在阅读了所有文章 帖子之后,我无法使分页工作。分页从来没有显示出来。
在使用页面模板查询没有成功后,我尝试了archive-news.php方法,在那里它会自动显示自定义的文章类型。分页在那里确实有效。使用此方法的缺点是没有'实际'页面与之绑定(这也将具有自定义字段,能够很好地添加到菜单中等)。
这是注册自定义文章类型的简化代码:
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,
));

然后是页面模板的代码:

$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 »');

任何想法出了什么问题?

2
请查看下一页链接页面,这里的示例将会有所帮助。http://codex.wordpress.org/Template_Tags/next_posts_link - Matt The Ninja
哇,就是这个。我简直不敢相信,在我进行了所有的 Codex 搜索之后,我居然没有找到它。谢谢!但愿我能将其作为答案接受。 - scferg5
已将其添加为答案以帮助其他人,因此不会显示为未回答。很高兴我能帮到你。 - Matt The Ninja
最后他使用了哪种方法,是archive-news.php方法还是带有模板的自定义页面方法,从未得到解释? - AGrush
2个回答

7
请查看下一页链接页面,这里的示例会有所帮助。codex.wordpress.org/Template_Tags/next_posts_link
<?php next_posts_link('Older Entries »', 0); ?>

WordPress Codex 示例。

        <?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; ?>

值得注意的是,当我将“$my_query”更改为“$wp_query”后,似乎修复了其他分页方法无法正常工作的一些问题(例如WP-PageNavi插件)。 - scferg5

6

分页,例如:上一页 1 2 3 下一页

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$news= new WP_Query(array(
    'post_type'=>'post',
    'posts_per_page' => 3,
    'paged' => $paged,
));

if($news->have_posts()) :
    while($news->have_posts())  : $news->the_post();
            the_title();
    endwhile;

    $total_pages = $news->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    ?>    
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

它显示分页,但当我点击第二个分页时,内容仍然相同。如何解决这个问题? - Chonchol Mahmud
请问您使用哪种永久链接结构? - Purvik Dhorajiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接