droos02800 2015-03-11 13:53
浏览 19

WordPress父/子页面过滤

I want to create a form with two fields. The first field lists all pages and the second field lists all child pages (if they exist) of the chosen page from the first field. When the form is submitted the site redirects the user to the chosen page. How can I implement this form?

  • 写回答

1条回答 默认 最新

  • dtiu94034 2015-03-11 14:09
    关注

    You're going to need to do an AJAX request to get the second drop down list.

    There is a function built into WordPress which gets the pages and you can specify parent ID's and also to show only top level pages.

    Heres an example of the code but you'll need to then implement it in a way that best suits your requirements

    <?php
    $top_level_pages = get_pages(array('parent'=> 0));
    ?>
    <select>
        <?php
        foreach($top_level_pages as $top_level_page) {
            echo '<option>'.$top_level_page['post_title'].'</option>';    
        }
        ?>
    </select>
    

    You will then need to determine what option the user has clicked and run an AJAX request to fill the second select.

    The second select should look something like this:

    <?php
    $id = $_GET['page_id']; // get the id of the page from the first select
    $child_pages = get_pages(array('child_of'=>$id));
    ?>
    <select>
        <?php
        foreach($child_pages as $child_page) {
            echo '<option>'.$child_page['post_title'].'</option>';    
        }
        ?>
    </select>
    

    Read more about the get_pages() function here: http://codex.wordpress.org/Function_Reference/get_pages

    评论

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程