WordPress.org

Ready to get started?Download WordPress

Forums

Display sub page content on parent page (3 posts)

  1. Calicosun
    Member
    Posted 4 years ago #

    Ok. I give up... again! After over 4 hours of trying to figure this out I need help please. Can someone please tell me what the php code is to display sub page (child) content on the parent page. Here is what I am trying to do:

    1. I have the parent pages as the main menu - horizontal at top of website. The parent page I am trying to set up is called services.

    2. I have the child page links in a side bar. The side bar is displayed on all pages of the website. I would like it to work so that if someone clicks on one of the child page links in the side bar ie. logos the content of the child page is displayed in the services parent page and ONLY that child page is displayed.

    3. If the visitor then decided to find out about 'brochures' they would click on the child page link in the sidebar called brochures and that sub page would come up in the services parent page.

    I am not having much luck getting help on the wordpress forum but perhaps I am posting in the wrong place... or not clearly asking my questions. I am doing my best but still learning. I would really appreciate help on this. Most of the information I have come across relates to listing the child page links on the parent page which is not what I want to do.

    Thanks so much in advance.

    Lisa

  2. equalmark
    Member
    Posted 4 years ago #

    I link to the website you are working on to provide a working example would help diagnose the problem. Hard to understand exactly what you are looking for with just your description.

    Try here:

    http://codex.wordpress.org/Template_Tags/wp_list_pages#List_Sub-Pages

  3. MichaelH
    Member
    Posted 4 years ago #

    So assuming this is what you want:

    "When displaying a child page, first want to display the content of the parent page above that child page".

    If that sound right, then using the WordPress Default theme as an example, take the pages.php and just before <?php if (have_posts()) : while (have_posts()) : the_post(); ?> add this code:

    <?php
    //if this is a child page, display the parent page's title and content
    if ($posts[0]->post_parent) {
      $type = 'page';
      $page_id = $posts[0]->post_parent;
      $args=array(
        'page_id'=> $page_id,
        'post_type' => $type,
      );
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'This is the parent page';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
          <?php
          the_content();
        endwhile;
      } //if ($my_query)
    } //if (post_parent)
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic