在wordpress中的几个页面上显示get_pages结果

时间:2014-09-26 19:03:54

标签: php wordpress

我正在使用以下的php来获取一个页面来显示其子页面的内容。 我想只显示前10个结果,然后再显示“显示更多”按钮。 我尝试用post_excerpt(L6)替换post_content,但这不起作用。我错过了什么?

<?php
    $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );

    foreach( $mypages as $page ) {      
        $content = $page->post_content;
        if ( ! $content ) // Check for empty page
            continue;

        $content = apply_filters( 'the_content', $content );
    ?>
        <h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
        <div class="entry"><?php echo $content; ?></div>
    <?php
    }   
?>

1 个答案:

答案 0 :(得分:0)

您需要告诉get_pages您想要的页码。但是,查看the get_pages documentation我没有看到任何明确的页码参数;相反,我怀疑你会想要offset参数。考虑尝试:

$mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc', 'offset' => ( $posts_per_page * $paged ) ) );

'offset' => ( $posts_per_page * ( $page_number - 1) )计算将全局$posts_per_page(在您的阅读设置中定义)与您以某种方式提供的$page_number相乘。页码减1,因为您想从指定点开始;如果你每页有10个帖子并希望从​​第二页开始,你想要显示的第一个帖子是索引#10(使用零索引列表),并通过#19显示。

您如何选择识别请求的页面由您自行决定,但一个选项是提供查询字符串参数(例如pagenumber=3)并在调用get_pages之前通过声明:

$page_number = is_numeric( $_GET['pagenumber']) ? $_GET['pagenumber'] : 0 )