【问题标题】:Get all child pages in wordpress without the parents在没有父母的情况下获取wordpress中的所有子页面
【发布时间】:2016-05-23 13:53:24
【问题描述】:

我的 wordpress 网站有这样的页面:

-home
   -about
   -history
-contact
   -office1
   -office2
   -office3
-solutions
   -corporate

菜单结构也是一样的。如何仅将子页面作为数组获取,以便获得如下内容:

-about
-history
-office1
-office2
-office3
-corporate

我想要所有深度为 1 的页面。现在我正在使用这个功能:

$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
   $pages[] += $page->ID;
}

但在这里我也得到了父页面。我可以做一个循环来从数组中删除父页面,但是有没有任何 wordpress 解决方案可以在一个函数调用中得到我想要的?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您可以使用 WP_Query 对象来做到这一点:

    $children_query = new WP_Query(array(
        'post_type'           => 'page',
        'orderby'             => 'menu_order',
        'order'               => 'ASC',
        'post_parent__not_in' => array('0')
    ));
    $children_pages = array();
    if($children_query->have_posts()){
        while($children_query->have_posts()){
            $children_query->the_post();
            $children_pages[] = get_the_ID();
        }
        wp_reset_postdata();
    }
    var_dump($children_pages);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-27
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多