WordPress.org

Ready to get started?Download WordPress

Forums

Showing list of subpages on pages (8 posts)

  1. trejder
    Member
    Posted 4 years ago #

    Hi there,

    I'm constructing a WP-based blog that contains many information on pages instaead of post, layed in a nice layout of pages and subpages. Is it possible (and how to achieve it - by modifying theme PHP code or using some function) to list all subpages belonging to current page when this page is displayed?

    For example, I have:

    - Info
    -- About
    -- Contact
    - My works
    -- Photo
    -- WWW

    Info and My works are empty now as they are only placeholders for subpages. I would like to list About and Contact when user select Info and Photo and WWW when displays My works.

    Of course I can add each new subpage manually on its ancestor page. But in fact I forgot about it most of the time and think that this could be somehow automated?

    Regards,
    Trejder

  2. MichaelH
    Member
    Posted 4 years ago #

    One possibility is the plugin http://wordpress.org/extend/plugins/template-tag-shortcodes/ along with the shortcode:

    [wp_list_pages]

    Or you would need to use the template tag, wp_list_pages() in a Page template.

    In either case you will use the child_of=x argument.

  3. scotchegg78
    Member
    Posted 4 years ago #

    uck plugins for this :)

    Get your hands dirty ...

    use this code in your theme file...

    <div id="submenu">
    <ul>
    <?php
    if($post->post_parent)
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    else
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    <?php echo $children; ?>
    <?php } ?>
    </ul>
    </div>
  4. trejder
    Member
    Posted 4 years ago #

    scotchegg78,

    The solution you've provided works perfectly except for the fact that it only displays subpages titles as link to them. How to modify above code to have actually displayed title and lead (part of each page before pressing "Insert read more tag) - text that in post is displayed before "Read more" link?

  5. MichaelH
    Member
    Posted 4 years ago #

    Look at using Function_Reference/get_pages <---there's examples in that article.

  6. Ovidiu
    Member
    Posted 4 years ago #

    @MichaelH:

    I'd love to use that plugin, but this short code: [wp_list_pages] lists all pages, not only the subpages of the current page...

  7. atomik
    Member
    Posted 3 years ago #

    @trejder: Hallo, did you find a solution at the end? I'm struggling with the same issue...

  8. keesiemeijer
    moderator
    Posted 3 years ago #

    put this function in your functions.php:

    function my_page_exerpts($parentID = false, $limit) {
       if($parentID) {
       $pages = get_pages('child_of='.$parentID);
       $limit = ($limit == '') ? 40 : $limit;
       $html = '';
         if(!empty($pages)) {
           foreach($pages as $page) {
           $mycontent = $page->post_content;
           $mycontent = strip_shortcodes($mycontent);
           $mycontent = str_replace(']]>', ']]>', $mycontent);
           $mycontent = strip_tags($mycontent);
           $contentTemp = explode(' ', $mycontent);
             if (count($contentTemp) > $limit) {
               $mycontent = implode(' ', array_slice($contentTemp, 0, $limit));
             }
           $html .= '<h2><a href="' .get_page_link($page->ID) . '" title="';
           $html .=  $page->post_title . '">' .$page->post_title . '</a></h2>';
           $html .= '<p>' . $mycontent . '</p>';
           $html .= '<a href="' . get_page_link($page->ID) . '" ';
           $html .= 'title="read more about: ' . $page->post_title . '" >';
           $html .= 'Read More</a>';
           }
         }
       }
       return $html;
    }

    And call this function in your page.php template file:

    <?php
      if($post->post_parent == 0){
      echo my_page_exerpts($post->ID, 20);
      }
    ?>

    The second value (20) is how long your excerpt is going to be. In the example it is 20 words long

Topic Closed

This topic has been closed to new replies.

About this Topic