• My site is about videogames so i have the main systems and the subpages are the names of all the games on that system.(Look at the sidebar http://pirategamer.com/systems/ )I need a way to hide the child categories unless i am in that section. What i mean is that unless im and on the psp section or on the page of a psp game, i will not see the psp games.
    Hope you understand what i am trying to do. so im looking for a way to only show child pages if in or under the parent page.

Viewing 11 replies - 1 through 11 (of 11 total)
  • This excessively complicated bit of code should do what you’re asking for:

    <?php
    global $wp_query, $wpdb;
    $pageid = $wp_query->post->ID;

    $subpages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_status = 'static' AND post_parent != ''");
    foreach($subpages as $subpage) :
    if(!($subpage->post_parent == $pageid)) :
    $exclude = $subpage->ID . ',';
    endif; endforeach;

    wp_list_pages("exclude=$exclude");
    ?>

    The above first performs an object query to collect the current Page’s ID. Then it queries the database to gather every Page with a parent. It adds each (child) Page ID to $exclude as long as the parent Page ID does not match the current one. Finally, $exclude is passed as the value for the wp_list_pages() ‘exclude’ parameter.

    Thread Starter pirategamer

    (@pirategamer)

    Do i put this in the sidebar, and does it matter where i put it. Do i need to edit any part of it or do i just paste it in. Thanks in advance.

    ——————
    I tried it in different spots but couldnt make it work i will put up my sidebar code because i am very sure thats where it goes

    [long code moderated]

    In future, please don’t post entire templates in the forums. It’s difficult to read, and only the relevant portion is necessary (in this case, the part setting up your wp_list_pages() display).

    Here is the section in your sidebar you probably want to replace:

    <?php /* Creates a menu for pages beneath the level of the current page */
    if (is_page() and ($notfound != '1')) {
    $current_page = $post->ID;

    …down to…

    Back to <?php echo $parent_title; ?>
    <?php } ?>
    </div>
    <?php } } ?>

    In its place add this version of the code I provided above (fixes a tiny bug and incorporates your features):

    <?php
    global $wp_query, $wpdb;
    $pageid = $wp_query->post->ID;

    $subpages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_parent != ''");
    foreach($subpages as $subpage) :
    if(!($subpage->post_parent == $pageid)) :
    $exclude .= $subpage->ID . ',';
    endif; endforeach;
    echo $exclude;
    ?>

    <div class="sb-pagemenu"><h2>Pages</h2>
    <ul>
    <?php wp_list_pages("sort_column=menu_order&title_li=&exclude=$exclude"); ?>
    </ul>
    </div>

    This should work as is, though you may want to alter the <h2> content, div class, etc.

    Thanks for the code Kafkaesqui. Unfortunately this doesn’t work for me (v2.0 – installed your code in sidebar). I get the following error:

    Warning: Invalid argument supplied for foreach() in /homepages/45/d155446623/htdocs/xxxxxxx/blog/wp-content/themes/warmup_allsidebar/sidebar.php on line 46

    I have also checked my database and although wordpress can differentiate Pages from Posts without problems the field “post_type” is blank in all cases.

    The field “post_parent” is not blank being either 0 or the parent ID.

    Your help would be appreciated.

    Post your sidebar template here:

    http://paste.uni.cc

    and reply back with the url to it.

    EDIT: My goof. Playing around with too many versions of WordPress right now. Change the $subpages line in the code above to:

    $subpages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_status = 'static' AND post_parent != ''");

    Note for the future: Previous coding of that line will most likely work in WordPress version 2.1. So when you upgrade and things go bonkers… ;)

    Thread Starter pirategamer

    (@pirategamer)

    Sorry about putting in all that code, im kinda a newb, but here is my sidebar at the url you said http://paste.uni.cc/8746
    now i see a long list of numbers in my sidebar, and when i got to the parent page i don’t see the children. i may have said it sort of weird but i want to see parent and “uncles”(i guess) with children, but not “grand children”

    if you go to this page you will see the numbers at the top of the side bar and you dont see the children http://pirategamer.com/systems/ and then if you click on ps3 or xbox 360 you dont see parent or children. so this code wored very good for the systems page but nowhere else.

    That long list of numbers is unfortunately a bit of debugging (the echo command) I forgot to remove.

    Anyway, here’s a revise of your sidebar.php:
    http://paste.uni.cc/8747

    Note my code works (correctly) ONLY IF one has a single layer of Pages/subPages, which is what I assumed your request involved. Having something like Page/subPages/subPages(/etc.) will get lost in my code’s parent Page tracking, which can only maintain a list of *all* Pages with a parent. It would require a more complex solution than my few lines above.

    (By the way, this thread is an example of my code pasting habits at their laziest. Sorry about that.)

    Thread Starter pirategamer

    (@pirategamer)

    Thanks anyway, it will help a little bit because now i wont see an annoyingly long list of pages. I will play around and do some searching to see if i can find any more info

    Thanks Kafkaesqui

    Works a treat.

    the solution seems to be perfect for me, unfortunatly i am a bit confused about your sidebar.php

    mine looks like this
    http://paste.uni.cc/8783

    do we talking about the sidebar.php in the theme folder or is there another one which i oversaw?

    thanks for your help

    dexter, the difference in sidebars is due to the theme used. All you (and most others) need be concerned with is this portion of your sidebar.php:

    <?php wp_list_pages(); ?>

    And here’s my hack-y replacement for that line:

    http://paste.uni.cc/8790

    Note I’ve slightly improved things so that now ‘siblings’ under a parent Page will continue to be listed when one is viewing one of them (before they just dropped back into the $exclude list).

    Note: If you–or anyone–is doing a bit of fancy post querying on Pages (using query_posts() or the like), this can scramble things as far as my code is concerned. In that case, add this to the very beginning of the code statement:

    rewind_posts();

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Get rid of child pages’ is closed to new replies.