【问题标题】:Wordpress custom post type loop pagination always go to homepageWordpress 自定义帖子类型循环分页总是转到主页
【发布时间】:2022-07-13 17:31:33
【问题描述】:

我有一个自定义帖子类型,我想通过分页循环。但是它显示正确,但是每当您单击分页(1、2、3、4 下一个和上一个)时,它总是会转到主页。我使用简码在页面上显示。提前致谢!


ob_start(); 全球 $post;

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

    $args = array (
        'post_type'     => 'ebooks-guides',
        'posts_per_page' => 4,
        'orderby'=> 'title',
        'order' => 'ASC',
        'paged' => $paged,
    
    );
    ?>
    <?php $the_query = new WP_Query( $args ); ?>
    <?php if (  $the_query->have_posts() )  : ?>
        <div class="ebooks-guides-container">
            <?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
                <div class="ebooks-guides">
                    <figure>
                        <?php the_post_thumbnail('single-post-thumbnail'); ?>
                    </figure>
                    <div class="ebooks-guides-description">
                        <h2><?php echo get_the_title(); ?></h2>
                        <?php the_content(); ?>
                        <?php
                            $link = get_field('webinar_link');
                            if( $link ):
                                $link_url = $link['url'];
                                $link_title = $link['title'];
                                $link_target = $link['target'] ? $link['target'] : '_self';
                                ?>
                                <a class="cta-button" href="<?php echo esc_url( $link_url ); ?>" target="<?php echo esc_attr( $link_target ); ?>"><?php echo esc_html( $link_title ); ?></a>
                            <?php endif; ?>
                    </div>
                </div>
            <?php endwhile;?>
        <div class="pagination">
            <?php
            
                $big = 999999999; // need an unlikely integer
                echo paginate_links( array(
                    'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                    'format'    => '?paged=%#%',
                    'current' => max( 1, get_query_var('paged') ),
                    'total'     => $the_query->max_num_pages,
                ) );
            ?>
        </div>
   
        
        </div>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    <?php
    return ob_get_clean();

【问题讨论】:

    标签: wordpress wordpress-theming custom-wordpress-pages wordpress-shortcode


    【解决方案1】:

    试试这个:

    $args = array (
        'post_type'     => 'ebooks-guides',
        'posts_per_page' => 4,
        'orderby'=> 'title',
        'order' => 'ASC',
        'paged' => get_query_var('paged'),
    
    );
    

    【讨论】:

    • 不幸的是,点击分页后仍然无法正常工作,它仍然会转到主页。
    【解决方案2】:

    在你的function.php文件中添加下面的函数

    //  Pagination function
    function pagination_bar( $custom_query ) {
    
        $total_pages = $custom_query->max_num_pages;
        $big = 999999999; // need an unlikely integer
    
        if ($total_pages > 1){
            $current_page = max(1, get_query_var('paged'));
    
            echo paginate_links(array(
                'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format' => '?paged=%#%',
                'current' => $current_page,
                'total' => $total_pages,
            ));
        }
    }
    

    并在循环完成后在您的视图区域中添加以下代码。

    echo pagination_bar( $the_query);
    

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 2011-03-20
      • 2014-05-21
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 2014-07-12
      相关资源
      最近更新 更多