【问题标题】:How can I get the child pages of multiple page ID's in WordPress如何在 WordPress 中获取多个页面 ID 的子页面
【发布时间】:2014-11-14 14:49:17
【问题描述】:

抱歉,如果有人问过这个问题,但我确实搜索过它,但什么也没找到。我试图在同一个列表中输出多个页面 ID 的子页面,并且 only 以各种方式输出这些页面中的 6 个。换句话说,我想获取超过一页的子页面并将结果混合在输出中。 现在我正在使用:

<?php $nbpages = 6; $pages = wp_list_pages('title_li=&sort_column=post_name&child_of=1041'); $pages_arr = explode("\n", $pages); for($i=0;$i<$nbpages;$i++){ echo $pages_arr[$i]; } ?>

这对于获取 一个 ID 的子页面很好,但我不知道如何添加一个数组来获取多个,然后混合结果。任何帮助表示赞赏!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您应该切换到使用WP_Query,它允许指定post_parent__in 参数。此参数接受页面或帖子 ID 的数组。注意:您需要为此运行 WP 3.6 或更高版本。

    $so26932595_query = new WP_Query( array(
        'posts_per_page' => 4,
        'post_type' => array( 'page' ),
        'orderby' => 'rand',
        'order' => 'none',
        'post_parent__in' => array( 1043, 1045, 1047, 1049, 1591 )
    ) );
    
    while ( $so26932595_query->have_posts() ) :
        $so26932595_query->the_post();
        echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
    endwhile;
    
    wp_reset_postdata();
    

    链接:http://codex.wordpress.org/Class_Reference/WP_Query

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 2011-12-04
      • 2020-04-20
      相关资源
      最近更新 更多