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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置