【问题标题】:Pagination in custom post type in wordpresswordpress中自定义帖子类型的分页
【发布时间】:2016-08-08 17:44:01
【问题描述】:

我正在尝试在 wordpress 中的自定义帖子中添加分页。我的自定义帖子类型名称是视频。它出现了分页,但是当我点击分页页面时,它会转到 404 页面。

<?php 
$videos= new WP_Query(array(
    'post_type'=>'videos',
    'posts_per_page' => 9,

));?>


<?php if($videos->have_posts()) : ?>
    <?php while($videos->have_posts())  : $videos->the_post(); ?>
        <div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
            <div class="video">
                <?php the_post_thumbnail(); ?>
                <div class="watch">
                    <a href="<?php the_permalink(); ?>"><i class="fa fa-play"></i></a>
                </div>
            </div>
            <div class="video-exerpt">
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </div>
        </div>

    <?php endwhile; ?>
    <div class="col-xs-12 text-center">
        <?php
            $GLOBALS['wp_query'] = $videos; 

            the_posts_pagination(
                array(
                    'mid_size' => '2',
                    'prev_text' => '<i class="fa fa-hand-o-left"></i> Previous',
                    'next_text' => 'Next <i class="fa fa-hand-o-right"></i>',
                    'screen_reader_text' => ' '
                    )
            );
        ?>
    </div>
<?php else :?>
    <h3><?php _e('404 Error&#58; Not Found', 'Bangladesh Parjatan'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

它显示分页 bt 链接不起作用。请帮帮我。

【问题讨论】:

标签: php wordpress pagination custom-post-type


【解决方案1】:

像这样传递你的 wp_query 参数。您应该使用 paged 参数进行分页。

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $videos= new WP_Query(array(
                        'post_type'=>'videos',
                        'posts_per_page' => 9,
                        'paged' => $paged,
                    ));

希望你的分页可以正常工作。

【讨论】:

  • 当我点击第二页时,它会在按钮 bt 中显示链接,它会转到 404.php 页面......请看截图dropbox.com/s/nzu8zxz6x9yymhz/Captsldnure.JPG?dl=0
  • 您的帖子类型名称和您的页面 slug 是否相同?
  • 如果相同,则从您的帖子类型名称更改您的页面 slug。
  • @saift0014 你有解决办法吗?
  • @saift0014 好的,如果回答有帮助请采纳。
【解决方案2】:

你能替换下面的代码吗?

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

$videos= new WP_Query(array(
    'post_type'=>'videos',
    'posts_per_page' => 9,
    'paged' => $paged,
)); ?>

<?php if($videos->have_posts()) : ?>
<?php while($videos->have_posts())  : $videos->the_post(); ?>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
    <div class="video">
        <?php the_post_thumbnail(); ?>
        <div class="watch">
            <a href="<?php the_permalink(); ?>"><i class="fa fa-play"></i></a>
        </div>
    </div>
    <div class="video-exerpt">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </div>
</div>

<?php endwhile; ?>            
    <?php 
    $total_pages = $videos->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', 'Bangladesh Parjatan'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

【讨论】:

    【解决方案3】:

    试试下面的代码:

       
    
    <?php 
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $videos= new WP_Query(array(
        'post_type'=>'videos',
        'posts_per_page' => 9,
        'paged' => $paged,
    )); ?>
    
    <?php if($videos->have_posts()) : ?>
    
    <?php while($videos->have_posts())  : $videos->the_post(); ?>
    
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
    
        <div class="video">
        
            <?php the_post_thumbnail(); ?>
            
            <div class="watch">
            
                <a href="<?php the_permalink(); ?>"><i class="fa fa-play"></i></a>
                
            </div>
            
        </div>
        
        <div class="video-exerpt">
        
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            
        </div>
    
    
    <?php endwhile; ?>            
        
    <?php else :?>
    
    <h3><?php _e('404 Error&#58; Not Found', 'Bangladesh Parjatan'); ?></h3>
    
    <?php endif; ?>
    
    </div>
    
    <nav>
    								<ul class="pagination">
                    
    									<?php
                      
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
    ) );
                                        ?>
    								</ul>
    							</nav>
    
    <?php wp_reset_postdata();?>

    【讨论】:

    • 你可能有一个正确的答案,但请在回答问题时解释你的代码
    猜你喜欢
    • 2014-05-15
    • 2011-03-20
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2012-11-29
    • 2017-06-04
    • 1970-01-01
    相关资源
    最近更新 更多