WordPress中如何获取父页面下的所有子页面列表

前端之家收集整理的这篇文章主要介绍了WordPress中如何获取父页面下的所有子页面列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在使用的过程当中,有些时候,我们需要循环输出父页面下的所有子页面信息。当然我们可以通过安装插件来实现,但是这么简单的功能也去下载插件,安装插件,未免有点小题大做了,下面我们介绍一下不使用插件通过代码实现的方法。

1. 切换到主题目录,打开functions.php文件,添加如下代码:

Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-2">2
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-4">4
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-6">6
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-8">8
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-10">10
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-12">12
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-13">13
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-14">14
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-15">15
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-16">16
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-17">17
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-18">18
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-19">19
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-20">20
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-21">21
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-22">22
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-23">23
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-24">24
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-25">25
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-26">26
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-27">27
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-28">28
Crayon-num" data-line="Crayon-5c891d545e6c7118389185-29">29
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d545e6c7118389185-30">30
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d545e6c7118389185-1">Crayon-t">functionCrayon-h"> Crayon-e">wp_get_all_children_pagesCrayon-sy">(Crayon-sy">)Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-2">Crayon-h"> Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-e">is_pageCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-sy">{
Crayon-line" id="Crayon-5c891d545e6c7118389185-3">Crayon-h"> Crayon-c">//当前页面的ID
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-4">Crayon-h"> Crayon-sy">$Crayon-v">current_obj_idCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">get_queried_object_idCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d545e6c7118389185-5">Crayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-6">Crayon-h"> Crayon-sy">$Crayon-v">argsCrayon-h"> Crayon-o">=Crayon-h"> Crayon-t">arrayCrayon-sy">(
Crayon-line" id="Crayon-5c891d545e6c7118389185-7">Crayon-h"> Crayon-s">'post_status'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'publish'Crayon-sy">,
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-8">Crayon-h"> Crayon-s">'post_type'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'page'Crayon-sy">,
Crayon-line" id="Crayon-5c891d545e6c7118389185-9">Crayon-h"> Crayon-s">'post_parent'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-v">current_obj_idCrayon-sy">,
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-10">Crayon-h"> Crayon-s">'orderby'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'menu_order'Crayon-sy">,
Crayon-line" id="Crayon-5c891d545e6c7118389185-11">Crayon-h"> Crayon-s">'order'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'ASC'Crayon-sy">,
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-12">Crayon-h"> Crayon-s">'nopaging'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-t">trueCrayon-sy">,
Crayon-line" id="Crayon-5c891d545e6c7118389185-13">Crayon-h"> Crayon-sy">)Crayon-sy">;Crayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-14">Crayon-h">
Crayon-line" id="Crayon-5c891d545e6c7118389185-15">Crayon-h"> Crayon-sy">$Crayon-v">child_pagesCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">get_postsCrayon-sy">(Crayon-sy">$Crayon-v">argsCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-16">Crayon-h"> Crayon-sy">$Crayon-v">htmlCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">""Crayon-sy">;
Crayon-line" id="Crayon-5c891d545e6c7118389185-17">Crayon-h"> Crayon-st">foreachCrayon-h"> Crayon-sy">(Crayon-sy">$Crayon-e">child_pages Crayon-st">asCrayon-h"> Crayon-sy">$Crayon-v">pageCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-18">Crayon-h"> Crayon-e">setup_postdataCrayon-sy">(Crayon-sy">$Crayon-v">pageCrayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d545e6c7118389185-19">Crayon-h"> Crayon-sy">$Crayon-v">urlCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">get_permalinkCrayon-sy">(Crayon-sy">$Crayon-v">pageCrayon-v">IDCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-20">Crayon-h"> Crayon-sy">$Crayon-v">titleCrayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-v">pageCrayon-v">post_titleCrayon-sy">;
Crayon-line" id="Crayon-5c891d545e6c7118389185-21">Crayon-h"> Crayon-sy">$Crayon-i">htmlCrayon-h"> Crayon-sy">.Crayon-o">=Crayon-h"> Crayon-e">sprintfCrayon-sy">(Crayon-s">"%s"Crayon-sy">,Crayon-h"> Crayon-sy">$Crayon-v">urlCrayon-sy">,Crayon-h"> Crayon-sy">$Crayon-v">titleCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-22">Crayon-h"> Crayon-sy">}
Crayon-line" id="Crayon-5c891d545e6c7118389185-23">
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-24">Crayon-h"> Crayon-e">wp_reset_postdataCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d545e6c7118389185-25">Crayon-h"> Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-o">!Crayon-e">emptyCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-i">htmlCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-26">Crayon-h"> Crayon-i">echoCrayon-h"> Crayon-sy">$Crayon-v">htmlCrayon-sy">;
Crayon-line" id="Crayon-5c891d545e6c7118389185-27">Crayon-h"> Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-28">Crayon-h"> Crayon-sy">}
Crayon-line" id="Crayon-5c891d545e6c7118389185-29">
Crayon-line Crayon-striped-line" id="Crayon-5c891d545e6c7118389185-30">Crayon-sy">}

2. 在page.PHP添加wp_get_all_children_pages()函数调用

这样我们就输出了父页面下的所有子页面标题列表,你还可以通过修改代码显示更多内容

原文链接:https://www.f2er.com/wordpress/238519.html

猜你在找的wordpress相关文章