WordPress.org

Ready to get started?Download WordPress

Forums

Displaying Children When Viewing 1 Child (19 posts)

  1. elmsblog
    Member
    Posted 7 years ago #

    I'm using this code to display subpages when viewing the parent page:

    <?php if(wp_list_pages("child_of=".$post->ID."&echo=0")) { ?>
    <ul>
    <?php wp_list_pages("title_li=&child_of=".$post->ID."&sort_column=menu_order&show_date=modified&date_format=$date_format");?>
    </ul>
    <?php } ?>

    When I get into a child page though, I still want all related children pages to still be listed. Right now the names disappear because there are no children pages of the one I'm viewing.

    Any suggestions?

  2. Kafkaesqui

    Posted 7 years ago #

    I banged out a solution to something similar on a little project some months back. Below is what I used to detect if a Page is parent or child, and pass the proper ID (for the 'child_of' argument) from the $wp_query object, that being post->ID if a parent, post->post_parent if a child.

    Feel free to play with it:

    <?php
    global $wp_query;

    if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
    } else {
    $parent = $wp_query->post->post_parent;
    }

    wp_list_pages("title_li=&child_of=$parent");
    ?>

  3. elmsblog
    Member
    Posted 7 years ago #

    PERFECT!

    I'm not a coder, so 1 last question. If the page is a parent OR a child, then I want to show the parent's page title in my menu above the listed subpages.

    I tried a ton of if statments and just guessed over and over, but couldn't figure it out. Anyone know this one?

  4. xinfo
    Member
    Posted 7 years ago #

    I am looking for same option for post i want to show only child category when viewing particular category

  5. Kafkaesqui

    Posted 7 years ago #

    "I want to show the parent's page title in my menu above the listed subpages."

    It would require a query to the database in the case of a child Page, but this mod to the code above will do it:

    <?php
    global $wp_query;

    if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
    $parent_title = $wp_query->post->post_title;
    } else {
    $parent = $wp_query->post->post_parent;
    $parent_title = $wpdb->get_var("SELECT post_title FROM $wpdb->posts WHERE ID = $parent");
    }
    ?>

    <h2><?php echo apply_filters('the_title', $parent_title); ?></h2>
    <?php wp_list_pages("title_li=&child_of=$parent"); ?>

  6. Kafkaesqui

    Posted 7 years ago #

    "I am looking for same option for post i want to show only child category when viewing particular category"

    xinfo, this is a very different topic you're requesting help for (though it may *seem* similar), and if you haven't already I'd suggest starting a new forum thread on it.

  7. elmsblog
    Member
    Posted 7 years ago #

    Oh so close! This code is going in my sidebar, and if I'm not viewing a page or sub-page, then it should not display anything. When viewing my homepage or any regular post, it is showing the post's title. *I need to add a 'ul' tag to format the wp_list_pages, but I'll get to that.

    http://www.tuesdaytourney.com (look in the top of the sidebar). Click on the 'about' page at the top of the window and you'll see my subpages.

    Thanks for your help ya'll! I'm studying the code as we work through this so I can figure out how it all works. This stuff is so cool.

  8. Kafkaesqui

    Posted 7 years ago #

    "This code is going in my sidebar, and if I'm not viewing a page or sub-page, then it should not display anything."

    You can restrict it through use of the is_page() conditional:

    <?php if( is_page() ) : ?>

    Code to display on Pages goes here

    <?php endif; ?>

    See the info on conditional tags for more:

    http://codex.wordpress.org/Conditional_tags

  9. elmsblog
    Member
    Posted 7 years ago #

    You're a genius. I'm trying think inside the query, and you thought outside the box. Good job and 1,000 'Thank You's.

  10. peiqinglong
    Member
    Posted 7 years ago #

    PERFECT! You don't know how long I've been racking my brain trying to figure this one out! Thank you! Thank you!

  11. SB
    Member
    Posted 7 years ago #

    *I need to add a 'ul' tag to format the wp_list_pages, but I'll get to that.

    may i ask what you mean by this code. i have tried to copy/paste the code above and i received a <h2>title from my recent post<h2> in the sidebar.

    In other words, elmsblog example didnt work. On his web page, you can only view the subpages attached to the main page and not on his main index page.

  12. skennedy1217
    Member
    Posted 7 years ago #

    I am using the following to show child pages...

    <?php
    global $wp_query;

    if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
    } else {
    $parent = $wp_query->post->post_parent;
    }

    wp_list_pages("title_li=<h2>Subpages</h2>&child_of=$parent");
    ?>

    How can I set it so that the parent page title precedes the heading of Subpages?

    For example:

    About Subpages
    -Vision
    -Mission
    -Strategy

  13. skennedy1217
    Member
    Posted 7 years ago #

    Kafkaesqui, on your post dated 2006-10-08 01:13:52, I am trying to use that code and it seems to work...until I click on one of the child pages. Then I get the following error:

    Fatal error: Call to a member function on a non-object in /home/bygra2/public_html/wp-content/plugins/widgets/execphp.php(24) : eval()'d code on line 9

  14. skennedy1217
    Member
    Posted 7 years ago #

    Sorry for all the replies, but I think I found how to do what I wanted to do. Let me first explain the situation.

    -WP 2.1 installation
    -Using a 'static' page as the home page (new to WP 2.1)
    -The second link in the header menu (News)is the "blog" section
    -The remaining static pages have multiple child pages

    I wanted to display the child page links on the sidebar only when one of the parent page links on the header menu were clicked. I also wanted to show all of the links for the child pages as you clicked through them. Furthermore, I wanted to show a header title above the links that showed the name of the parent page.

    The only caveat, which is the problem I kept running into with some of the other solutions on this thread, is that I did not want to show any parent page names on the "home" and "news" pages of the site. For example, with one solution I clicked on the news/blog section and the top post title was showing on the top of the sidebar.

    So, here is the solution I came up with. It's a lot of trial and error (I'm not a programmer). If anyone can think of a better way to do this then please suggest it. As it stands, I'm going to have to make manual changes here as I add pages. BTW, I'm also running this in a PHP Sidebar Widget (ExecPHP).

    http://www.bygraceforglory.com

    <?php
    if (is_page('2')) {
    echo "<h2>About</h2>";
    } elseif (is_page('18')) {
    echo "<h2>About</h2";
    } elseif (is_page('19')) {
    echo "<h2>About</h2";
    } elseif (is_page('20')) {
    echo "<h2>About</h2";
    } elseif (is_page('21')) {
    echo "<h2>About</h2";
    } elseif (is_page('22')) {
    echo "<h2>About</h2";
    } elseif (is_page('5')) {
    echo "<h2>Ministries</h2";
    } elseif (is_page('16')) {
    echo "<h2>Ministries</h2";
    }
    ?>
    <?php
    global $wp_query;
    if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
    } else {
    $parent = $wp_query->post->post_parent;
    }
    ?>
    <ul>
    <?php wp_list_pages("title_li=&child_of=$parent"); ?>
    </ul>

  15. thekelpie
    Member
    Posted 7 years ago #

    Thanks to Kafkaesque for the above code. I'm using it with the K2 theme to create a subpage menu under the main nav bar. I've got it working nicely, but there's just one niggling thing: when I'm on a subpage, I'd like its parent page (as well as the subpage itself) to be highlighted in the main nav bar, for a sort of breadcrumb effect. The stylesheet tells the menu item for the current page to have a certain background colour - but is there a way of also making the parent menu item a particular colour?

    Is this even possible? Thanks again for a really useful piece of code.

  16. thekelpie
    Member
    Posted 7 years ago #

    Okay, for anyone else who comes across this thread, I managed to get what I wanted using the fold_page_list plugin and adding some css for page ancestors.

  17. winkelried
    Member
    Posted 6 years ago #

    Kafka is GOD!

    Thanks a lot for the code at "Feel free to play with it:"

    YOU made my day!!!

  18. katootje
    Member
    Posted 6 years ago #

    I am very happy with this thread. I manage with this code and the fold_page plugin and some others things to display the parent pages in the header of my website. Selecting the parent with children, I get the list of children in the sidebar, selecting one of the children, the parent is still in active mode in the topnavigation, by showing another color. So far perfect. but now the owner of the site wants grandchildren, subpages of the subpages. Now I get the following problem. Everything is okay till I select a grandchild in the sidebar. Then the grandchild becomes child and his parent becomes parent instead of staying the child. In the main navigation, the parent is no longer selected and in the sidebar are only two levels instead of three.
    So normally you have:
    parent
    - child
    - child2
    -- grandchild1
    -- grandchild2

    and the parent is also selected in the topnavigation

    Clicking on grandchild1 gives:
    child
    - grandchild1
    - grandchild2

    parent is no longer selected in the topnavigation

    What can i do to show the first list in the side bar when selecting a grandchild and also let the parent in the main navigation stay active.

    In the sidebar I have used the code given above.
    The parent stays selected by the following code in the css:
    #navbar .current_page_item a, #navbar .current_page_item a:visited, #navbar .current_page_parent a, #navbar .current_page_parent a:visited{etc etc

    Hope there is someone who can help me with this.

    Karin

  19. jdlejeune
    Member
    Posted 6 years ago #

    I love you.

Topic Closed

This topic has been closed to new replies.

About this Topic