首页 > 解决方案 > 用页面 wordpress 填充数组

问题描述

我的网站上有一个产品页面,我有一个数组来存储所有以产品为父级的页面。这工作正常,但由于某种原因,它在 10 个条目后停止填充数组。这是我填充数组的代码。

$args = array(
    'post_parent' => get_page_by_path( 'products' )->ID,
    'post_type'   => 'page',
    'numberposts' => -1,
    'post_status' => 'any');    

// The Query
$parent = new WP_Query( $args );
$n = 0;
// The Loop
if ( $parent->have_posts() ) {
    echo '<ul>';
    while ( $parent->have_posts() ) {
        $parent->the_post();
        echo '<li>' . get_the_title() . '</li>';
        $n++;
        echo $n;
    }
    echo '</ul>';
    // Restore original Post Data
    wp_reset_postdata();
}

这将返回页面的标题并回显一个数字来计算它们。输出显示标题,但在 之后停止10 pages,即使我有12 pages产品作为父级。奇怪的是,当我添加另一个以产品为父页面的页面时,它会将列表中的最后一个页面推出,并将新添加的页面放在数组的第一个位置。欢迎任何帮助!

标签: phphtmlwordpress

解决方案


使用posts_per_page而不是numberposts.

$args = array(
'post_parent' => get_page_by_path( 'products' )->ID,
'post_type'   => 'page',
'posts_per_page' => -1,
'post_status' => 'any'); 

推荐阅读