简体   繁体   English

自定义帖子类型存档,更多页面无效

[英]Custom post type archive, more pages don't work

I have a site on which a lot of video's will be posted next week. 我有一个网站,下周将发布很多视频。 The videos are posted with a custom post type, with just 'to', 'from' and yt video id. 视频以自定义帖子类型发布,仅带有“ to”,“ from”和yt视频ID。 I wan to load a lot the videos on an archieve page, but not more than x per page. 我希望在存档页面上加载很多视频,但每页不超过x。 So x posts on page 1, x on page 2 and so on. 因此,x在第1页上发布,x在第2页上,依此类推。 Unfortunately, the page number doesn't show up, the 'p' container just stays empty. 不幸的是,页码没有显示,“ p”容器只是保持空白。 Here is my code: 这是我的代码:

      $args = array( 'post_type' => 'video'/*, 'posts_per_page' => 3 */);
        $loop = new WP_Query( $args );
        if (have_posts()) : 
    while ( $loop->have_posts()) : $loop->the_post(); 


                <?php endwhile; ?>
                <p><?php posts_nav_link('&#8734;','&laquo;&laquo; View newer posts','View older posts &raquo;&raquo;'); ?></p>
                <?php endif; ?>

I have emptied the part between the while, because in that part I only call to basic functions like the_permalink and the_content. 我已经清空了这一段时间之间的部分,因为在那部分中,我仅调用了诸如the_permalink和the_content之类的基本函数。

As it turns out, using posts_nav_link in tandem with WP_Query doesn't work as expected, since posts_nav_link relies on the global $wp_query object, and doesn't recognize the custom WP_Query instance. 事实证明,将posts_nav_link与WP_Query一起使用不能按预期方式工作,因为posts_nav_link依赖于全局$ wp_query对象,并且无法识别自定义WP_Query实例。

This Answer should provide you the details you need in order to get everything to work. 该答案应为您提供所需的详细信息,以使一切正常运行。

<?php
global $wp_query;
$temp_wp_query = $wp_query;
$wp_query = null;

$paged = get_query_var('paged');
$args = array(
    'post_type' => 'video',
    'paged' => $paged ? $paged : 1
    /*, 'posts_per_page' => 3 */
);
$loop = new WP_Query( $args );
$wp_query = $loop;
if ($loop->have_posts()) : 
while ( $loop->have_posts()) : $loop->the_post(); 


<?php endwhile; ?>
<p><?php posts_nav_link('&#8734;','&laquo;&laquo; View newer posts','View older posts &raquo;&raquo;'); ?></p>
<?php
endif;
$wp_query = $temp_wp_query;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM