• Resolved fidra

    (@fidra)


    Hi everyone,

    I’d like to list all child pages of the current browsing page. But my URL is: https://www.domain.com/pageTitle

    I googled it and found the code which works with this type of URL: https://www.domain.com/?page_id=123

    I tried to use the_title(); to get the current page title and tried to convert it to string, then use the get_page_by_title(); to get the page ID.
    But it doesn’t work.

    This is my code:

    <!-- List child pages only -->
    <?php
    
    //$this_page_title = the_title();
    
    $this_page_title = 'the_title()';
    $this_page_id = get_page_by_title($this_page_title);
    
    if($post->post_parent)
    $children = wp_list_pages("depth=1&title_li&child_of=.$this_page_id->ID&echo=0"); 
    
    else
    
    $children = wp_list_pages("depth=1&title_li&child_of=.$this_page_id->ID&echo=0");
    
    if ($children) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>
    <!-- // End list -->

    Please help me!

    Thanks in advanced!

    PS: I don’t know what differences between .$this_page_id, .$this_page_id. and $this_page_id. Please explain.

    (sorry if my english bad ^^)

Viewing 11 replies - 1 through 11 (of 11 total)
  • Just use $this_page_title = get_the_title(); and $this_page_id = get_the_ID(); to get the title and ID. I changed your code for wp_list_pages a little, but I have not tested it.

    Also, the ‘else’ part of the if test is apparently doing the same thing as the if part, so it will serve no function.

    I don’t know if echo $children will display what you want. You may need to change that. What part of the child pages do you want to list?

    Can you post a link where you found the googled code? Maybe I can look at it and see more of what you want to do.

    <!-- List child pages only -->
    <?php
    
    $this_page_title = get_the_title();
    $this_page_id = get_the_ID();
    
    if($post->post_parent) {
       $children = wp_list_pages("depth=1&title_li&child_of=$this_page_id&echo=0"); 
    
    } else {
       $children = wp_list_pages("depth=1&title_li&child_of=$this_page_id&echo=0");
    }
    if ($children) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>
    <!-- // End list -->
    Thread Starter fidra

    (@fidra)

    This is the link that I found: https://www.martin-gardner.co.uk/how-to-list-wordpress-child-pages-only-on-that-parent-page/

    Please see the example: I had the following page structure
    – Paderborn
    Sub1
    Sub2
    – About
    Sub1
    — underSub1
    — underSub2
    Sub2

    I want to list the only the child pages of the current browsing page.
    For example: If I was browsing the Paderborn page, the Page list in the sidebar just lists the Sub1 and Sub2 page.

    I used pretty URL: https://www.domain.com/pageTitle

    Please help.

    Thread Starter fidra

    (@fidra)

    Thanks vtxyzzy very very much ??

    It works now, I used your code and it works now.

    Thank you again.

    Best regards,

    Thread Starter fidra

    (@fidra)

    Sorry vtxyzzy, it works. But now, I want it appeare like this:

    – When I browsing the About page, the Page list in the sidebar just lists all the child page (sub1, undersub1, undersub2, sub2).

    – When I browsing the About/Sub1 page, the Page list in the sidebar also lists all the child page (sub1, undersub1, undersub2, sub2) of the About page and hide all sub2’s child pages (if it has child page).

    – And if I browse the About/Sub2 page, the Page list in the sidebar also lists all the child page (sub1, sub2) of the About page and hide all sub1’s child pages.

    Can you help me?

    Thanks you very very much ??

    Did you try copying the code from the Google link exactly? It looks like that might work. The code you originally posted is different from the sample code and had several problems. What does the code from Google do?

    Thread Starter fidra

    (@fidra)

    Yes, the code from google works fine if I use the URL like this: https://www.domain.com/?p=123.

    But my URL is: https://www.domain.com/post-name, so it does not work. Then, I tried using your code and it works now.

    Everything work fine now.

    But can you show me how to make the Page list appear like this:

    – When I browsing the About page, the Page list in the sidebar just lists all the child page (sub1, sub2).

    – When I browsing the About/Sub1 page, the Page list in the sidebar also lists all the child page (sub1, sub1/undersub1, sub1/undersub2, sub2) of the About page and hide all sub2’s child pages.

    – And if I browse the About/Sub2 page, the Page list in the sidebar also lists all the child page (sub1, sub2, sub2/undersub1, sub2/unsersub2) of the About page and hide all sub1’s child pages.

    My page structure is:
    – Paderborn
    — Sub1
    — Sub2
    – About
    — Sub1
    — underSub1
    — underSub2
    — Sub2
    — underSub1
    — underSub2

    Please help! Thanks!

    – When I browsing the About page, the Page list in the sidebar just lists all the child page (sub1, sub2).

    This should be possible.

    – When I browsing the About/Sub1 page, the Page list in the sidebar also lists all the child page (sub1, sub1/undersub1, sub1/undersub2, sub2) of the About page and hide all sub2’s child pages.

    Don’t think this is possible, since Sub2 is not a child of About/Sub1.

    – And if I browse the About/Sub2 page, the Page list in the sidebar also lists all the child page (sub1, sub2, sub2/undersub1, sub2/unsersub2) of the About page and hide all sub1’s child pages.

    Same reason as above – Sub1 is not a child of About/Sub2.

    Please post a link to your site so I can work on the first part.

    Here is the best I can do:

    <?php
    if ( is_page() ) {
       if($post->post_parent) {
          $my_title = get_the_title($post->post_parent);
          $children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');
       } else {
          $my_title = $post->post_title;
          $children = wp_list_pages('depth=1&sort_column=menu_order&title_li=&child_of='.$post->ID.'&echo=0');
       }
       if ($children) {
    ?>
    <div class="sidebar">
    <h2>Sub-pages of <?php echo $my_title; ?></h2>
          <ul><?php echo $children; ?></ul>
    </div>
       <?php
       } // End If Children
    } // End of if is_page
    ?>

    If this is satisfactory, please mark this topic ‘Resolved’, otherwise explain what should be changed.

    One more change. If you would like the post title in ‘Sub-pages of title‘ to be a link, use this:

    <?php
    if ( is_page() ) {
       if($post->post_parent) {
          $my_title = get_the_title($post->post_parent);
          $my_link = get_permalink($post->post_parent);
          $children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');
       } else {
          $my_link = get_permalink();
          $my_title = $post->post_title;
          $children = wp_list_pages('depth=1&sort_column=menu_order&title_li=&child_of='.$post->ID.'&echo=0');
       }
       if ($children) {
    ?>
    <div class="sidebar">
    <h2>Sub-pages of <?php echo "<a href='$my_link' title='Link to $my_title'>$my_title</a>"; ?></h2>
          <ul><?php echo $children; ?></ul>
    </div>
       <?php
       } // End If Children
    } // End of if is_page
    ?>
    Thread Starter fidra

    (@fidra)

    Thanks for your help a lot!

    The code works fine. How can I use JavaScript to make the page list as a tree view. It means that when I click on About/Sub1 page, the Sub1 will expand and show all child pages when the Sub2 still collapse?

    My page was hosted on a free hosting, the host cannot access right now (I don’t know why. I tested the code on my localhost).

    The link is: https://hoangvuit.summerhost.info/wordpress/

    Hope it will be back soon ??

    Thanks for your help again!

    You are welcome. I do not know Java, so you should post a separate topic to get help on that.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Help] Get current page id base on page title’ is closed to new replies.