logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$20
Create sidebar menu

Hi,
I've a website where I mainly use pages, and there are 4 main pages, the rest are childs of those.
I want to display list of the childs in the sidebar depending on the page you are in.
I achieved to list them correctly by checking the ID using the code, but it only works in the parent page, I need it in the child page as well:

<ul>
<?php $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); if ($children) { ?>
<li class="widget">
<div class="title"><h5>Menu</h5></div>
<div class="content">
<ul><?php echo $children; ?></ul>
</div>
</li>
<?php } ?>
</ul>

on the other side I also need to have listed some of the other pages, for example:
if you are on page with ID 1 or it's childs, it will list on top (of the sidebar) 10 of the childs of ID 1, followed by 5 of ID 2, 5 of ID 3, and 5 of ID 4; then if you are on page with ID 2 or it's childs, it will list on top 10 of the childs of ID 2, followed by 5 of ID 1, 5 of ID 3, and 5 of ID 4, and so on for the other IDs.

Is this possible, can someone help me here

thank you

This question has been answered.

Karl | 07/28/12 at 3:29pm Edit


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    07/28/12
    3:35pm
    Martin Pham says:

    You can find answers in
    http://wpquestions.com/question/showLoggedIn/id/4265

    • 07/28/12 4:13pm

      Martin Pham says:

      Try this


      // Put on functions.php
      function page_menu_list() {
      global $post;
      if ($post->post_parent) {
      $parents = get_post_ancestors($post->ID);
      $parent = $parents[count($parents)-1];
      } else {
      $parent = $post->ID;
      }
      // Dislay curent page, child of curent page or parent
      $page_nav = wp_list_pages( 'title_li=&child_of='.$parent.'&echo=0' );
      // Display all top page (1-2-3-4)
      $all_top_page = wp_list_pages( 'title_li=&exclude_tree='.$parent.'&depth=1&echo=0' );
      printf('<div class="widget"><ul>%s</ul><ul>%s</ul></div>', $page_nav, $all_top_page);
      }


      Using


      <?php page_menu_list(); ?>

    • 07/28/12 4:32pm

      Martin Pham says:

      Update function



      function page_menu_list() {
      global $post;
      if ($post->post_parent) {
      $parents = get_post_ancestors($post->ID);
      $parent = $parents[count($parents)-1];
      } else {
      $parent = $post->ID;
      }
      // Dislay curent page
      $page_nav = wp_list_pages( 'title_li=&child_of='.$parent.'&echo=0' );
      // Display all top page (1-2-3-4)
      $all_top_page = wp_list_pages( 'title_li=&exclude_tree='.$parent.'&depth=1&echo=0' );
      printf('<div class="widget menu-page-widget">
      <h3 class="curent-parent-page"><a href="%s">%s</a></h3>
      <ul>%s</ul>
      <h3 class="other-top-page">Other Page</h3>
      <ul>%s</ul>
      </div>',
      get_page_link($parent),
      get_the_title($parent),
      $page_nav,
      $all_top_page
      );
      }


      Pls, View attachfile for example.

      Attached Image

    • 07/28/12 4:45pm

      Karl says:

      almost there

      It's listing the childs of current correctly, but as for the others, it's showing all the other parent pages of the website, it should list parent and child of specific ID
      coding speaking:
      if in page or child of ID1238, display 10 items of ID1238, 5 of ID218, 5 of 186, and 5 of 11
      and the same for the other IDs

    • 07/28/12 5:19pm

      Martin Pham says:

      Is this the structure of your page(ID)?

      •1238 (top page)
      -- xx
      -- xx
      ...
      -- xx
      • 218 (top page)
      • 186 (top page)
      • 11 (top page)

    • 07/28/12 5:23pm

      Karl says:

      more like
      •1238 (top page)
      -- xx
      -- xx
      -- xx
      ...
      -- xx (times 10)
      • 218 (top page)
      -- xx
      -- xx
      -- xx
      -- xx
      • 186 (top page)
      -- xx
      -- xx
      -- xx
      -- xx
      -- xx
      • 11 (top page)
      -- xx
      -- xx
      -- xx
      -- xx

    • 07/28/12 5:24pm

      Martin Pham says:

      OK!
      Please wait me :)

    • 07/28/12 5:24pm

      Karl says:

      "sorry, mistake"

      more like
      •1238 (top page)
      -- xx
      -- xx
      -- xx
      ...
      -- xx (times 10)
      • 218 (top page)
      -- xx
      -- xx
      -- xx
      -- xx
      -- xx
      • 186 (top page)
      -- xx
      -- xx
      -- xx
      -- xx
      -- xx
      • 11 (top page)
      -- xx
      -- xx
      -- xx
      -- xx
      -- xx

    • 07/28/12 5:53pm

      Martin Pham says:

      Done :)


      function page_menu_list() {
      global $post;
      if ($post->post_parent) {
      $parents = get_post_ancestors($post->ID);
      $parent = $parents[count($parents)-1];
      } else {
      $parent = $post->ID;
      }
      $top_pages = get_pages(array(
      'parent' => 0,
      'post_type' => 'page',
      'post_status' => 'publish'
      ));
      if($top_pages) {
      foreach( $top_pages as $top_page ) {
      if($top_page->ID == $parent) {
      child_of_page($top_page->ID, 10, true);
      }
      else {
      child_of_page($top_page->ID);
      }
      }
      }
      }
      function child_of_page($pageid = 0,$num = 5, $curent = false) {
      $args = array(
      'sort_column' => 'post_date',
      'sort_order' => 'desc',
      'hierarchical' => 1,
      'child_of' => $pageid,
      'number' => 5,
      'post_type' => 'page',
      'post_status' => 'publish'
      );
      $child = get_pages($args);
      echo '<div class="top_page_label '.($curent ? 'curent_page': '').'"><a href="'.get_page_link($pageid).'">'.get_the_title($pageid).'</a></div>';
      if($child) {
      echo '<ul class="top_page_list">';
      foreach( $child as $page ) {
      echo '<li><a href="'. get_page_link( $page->ID ).'">'. $page->post_title .'</a></li>';
      }
      echo '</ul>';
      }
      }


      Using for display:

      <?php page_menu_list(); ?>

    • 07/28/12 5:56pm

      Martin Pham says:

      sorry, im mistake. Replace this

      function page_menu_list() {
      global $post;
      if ($post->post_parent) {
      $parents = get_post_ancestors($post->ID);
      $parent = $parents[count($parents)-1];
      } else {
      $parent = $post->ID;
      }
      $top_pages = get_pages(array(
      'parent' => 0,
      'post_type' => 'page',
      'post_status' => 'publish'
      ));
      if($top_pages) {
      foreach( $top_pages as $top_page ) {
      if($top_page->ID == $parent) {
      child_of_page($top_page->ID, 10, true);
      }
      else {
      child_of_page($top_page->ID);
      }
      }
      }
      }
      function child_of_page($pageid = 0,$num = 5, $curent = false) {
      $args = array(
      'sort_column' => 'post_date',
      'sort_order' => 'desc',
      'hierarchical' => 1,
      'child_of' => $pageid,
      'number' => $num,
      'post_type' => 'page',
      'post_status' => 'publish'
      );
      $child = get_pages($args);
      echo '<div class="top_page_label '.($curent ? 'curent_page': '').'"><a href="'.get_page_link($pageid).'">'.get_the_title($pageid).'</a></div>';
      if($child) {
      echo '<ul class="top_page_list">';
      foreach( $child as $page ) {
      echo '<li><a href="'. get_page_link( $page->ID ).'">'. $page->post_title .'</a></li>';
      }
      echo '</ul>';
      }
      }

    • 07/28/12 6:04pm

      Karl says:

      Hi Martin,
      now it's showing pages randomly.

      Do you want me to send the link to the page by mail?

    • 07/28/12 6:08pm

      Martin Pham says:

      Yes, please send to email: marxvn@gmail.com

    • 07/29/12 12:10pm

      Karl says:

      Working good with

      <?php
      function page_menu_list($top_pages = array()) {
      global $post;
      if ($post->post_parent) {
      $parents = get_post_ancestors($post->ID);
      $parent = $parents[count($parents)-1];
      } else {
      $parent = $post->ID;
      }
      // Display current on top menu page
      if(in_array($parent, $top_pages)) {
      get_item_page($parent, 15, true);
      // Child menu item
      foreach( $top_pages as $top_page ) {
      if($top_page != $parent) {
      get_item_page($top_page);
      }
      }
      }
      }

      function get_item_page($parent = 0, $num = 5, $curent = false) {
      $query_args = array(
      'post_type' => 'page',
      'post_parent' => $parent,
      'showposts' => $num,
      'orderby' => 'date',
      'order' => 'DESC'
      );
      echo '<div class="top_page_label'.($curent ? ' curent_page': '').'"><a href="'.get_page_link($parent).'">'.get_the_title($parent).'</a></div>';
      $item = new WP_Query( $query_args );
      if ( $item->have_posts() ) {
      echo '<ul class="top_page_list">';
      while ( $item->have_posts() ) : $item->the_post();
      printf(
      '<li><a href="%s" title="%s" >%s</a></li>',
      get_permalink(),
      the_title_attribute( 'echo=0' ),
      get_the_title()
      );
      endwhile;
      echo '</ul>';
      }
      wp_reset_query();
      }
      ?>

  • avatar
    Last edited:
    07/28/12
    3:40pm
    Arnav Joy says:

    you have to slightly modified your function to display it on child pages as well

    <ul>		 

    <?php $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); if ($children) { ?>

    <li class="widget">

    <div class="title"><h5>Menu</h5></div>

    <div class="content">

    <ul><?php echo $children; ?></ul>

    </div>

    </li>

    <?php } else { $children = wp_list_pages('title_li=&echo=0'); ?>

    <li class="widget">

    <div class="title"><h5>Menu</h5></div>

    <div class="content">

    <ul><?php echo $children; ?></ul>

    </div>

    </li>

    <?php } ?>
    </ul>


    for the second point , please explain it more clearly

    • 07/28/12 3:53pm

      Karl says:

      it works, but when in the child page it's listing all pages of the website, and not the pages related to parent page only

      To clear: when Im in page ID1, it's listing the childs, if I'm in a child of ID 1, it should still show the same menu as in the parent


    • 07/28/12 3:59pm

      Arnav Joy says:

      paste this function in functions.php

      <?php
      //return the name of the top parent page base on a page_id
      function getTopParentPostName($myid){
      $mypage = get_page($myid);

      if ($mypage->post_parent == 0){
      return $mypage->post_name;
      }
      else{
      return getTopParentPostName($mypage->post_parent);
      }

      }?>


      then modify your original code as follows:-

      <ul>		 

      <?php $children = wp_list_pages('title_li=&child_of='.getTopParentPostName($post->ID).'&echo=0'); if ($children) { ?>

      <li class="widget">

      <div class="title"><h5>Menu</h5></div>

      <div class="content">

      <ul><?php echo $children; ?></ul>

      </div>

      </li>

      <?php } ?>

      </ul>


  • avatar
    Last edited:
    07/28/12
    3:51pm
    spencert says:

    Personally, I'd take the lazy approach to this as opposed to writing code.
    I'd install WooDojo and then it's sidebars manager.
    I'd create the menus with the built in Wordpress menu's and control which menu appears on which page with the sidebar manager.

    Done. No code, effective, easy to maintain and make changes to without a single line of code.

    • 07/28/12 3:56pm

      Karl says:

      thanks, but I'm trying to avoid plugins as much as I can :)

This question has expired.



Hai Bui, Karl, Arnav Joy, Manoj Raj voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.