dtjpz48440 2011-12-30 14:22
浏览 106
已采纳

Wordpress为显示子页面的自定义循环添加分页

I have one page (not an article) with n subpages. In the main page, I need to show max 3 titles of the subpages and insert a pagination for the other.

How can I do that?

This is my simple code now:

<?php 
    $parent_id = 14; //main page id
    $pages = get_pages( array( 'sort_column' => 'menu_order', 'numberposts' => 3, 'child_of' => $parent_id ) );
    foreach ( $pages as $page ) : ?>
        <div class="item">
            <div class="item-title">
                <h2><a href="<?php echo get_permalink( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
            </div>
        </div>
    <?php endforeach; ?>

Thanks.

  • 写回答

3条回答 默认 最新

  • dowjgrm6787 2011-12-31 12:34
    关注

    I solved by myself, the solution is to use wp_query() to create a new loop insted of using get_pages().

    Here the new code for page title and contentwith pagination by Preeti Dua from Avigma Technology:

        <?php
    
        // Pagination variable
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        // The Query
        $the_query = new WP_Query( array( 'post_parent' => 782, 'post_type' => 'page', 'paged' => $paged) );
    
        // The Loop
        if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post();
        global $post;
        $thePostID = $post->ID; /* this variabled is used if you need to get custom fields of the subpage */
            ?>
        <div id="side-post-title">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </div>
    
         <div id="side-post-excerpt">
                   <?php the_excerpt(); ?>
    
                 <a href="<?php echo get_permalink( $page->ID ); ?>"> <div id="read-more"> 
                           <img src="/wp-content/uploads/2012/10/read-more-btn.png"/></div> </a>                                                          
          </div> 
    
    
    
    <?php endwhile; endif; ?>
    
    <nav class="navigation">
       <div style="float:left;"> <?php next_posts_link('Show older', $the_query->max_num_pages) ?></div>
       <div style="float:right;"> <?php previous_posts_link('Show newer') ?></div>
    </nav>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库