Forums

Display every grandchildren (18 posts)

  1. baga
    Member
    Posted 1 year ago #

    I'm using this code to get a random list of pages:

    <?php
    $postid = $post->ID;
    $args = array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'orderby' => 'rand',
    'showposts' => 5,
    'post__not_in' => array($postid)
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;
    }
    wp_reset_query();
    ?>

    This shows all pages, but I would like to display not all of them, just pages which are granchildren, how can I do that?

  2. baga
    Member
    Posted 1 year ago #

    I try to explain myself better, here's an example:

    Parent
    - Child 1
    - - Grandchild 1.1
    - - Grandchild 1.2
    - - Grandchild 1.3
    - Child 2
    - - Grandchild 2.1
    - - Grandchild 2.2
    - - Grandchild 2.3
    - Child 3
    - - Grandchild 3.1
    - - Grandchild 3.2
    - - Grandchild 3.3

    How can I display all the grandchildren without parent page and child being shown? Something like this:

    - - Grandchild 1.1
    - - Grandchild 1.2
    - - Grandchild 1.3
    - - Grandchild 2.1
    - - Grandchild 2.2
    - - Grandchild 2.3
    - - Grandchild 3.1
    - - Grandchild 3.2
    - - Grandchild 3.3

  3. baga
    Member
    Posted 1 year ago #

    Anyone can help?

  4. baga
    Member
    Posted 1 year ago #

    :-( :-( :-(

  5. alchymyth
    The Sweeper
    Posted 1 year ago #

    for instance:

    http://pastebin.com/ngnuunG6

    first step:
    get list of children of the page;
    second step:
    (foreach) loop through those pages;
    third step:
    get their children and show in loop.

  6. baga
    Member
    Posted 1 year ago #

    Thanks alchymyth for your help :)
    It did not work though: it doesn't display anything.

    I tried to remove this part:

    'post_parent' => $postid

    and then it shows a list of all children and granchildren, I just need not to display the children but only grandchildren.

  7. alchymyth
    The Sweeper
    Posted 1 year ago #

    where are you running the code?

    in that location, $post might not be available, because it got distorted by a custom query?

    what hapens if you enter a know page ID (which has children and grandchildren) into that line (example):
    'post_parent' => 57

  8. baga
    Member
    Posted 1 year ago #

    I'm running the code on a template used for all pages which are grandchildren and there aren't other queries on that template. Cannot enter a known page ID.

  9. alchymyth
    The Sweeper
    Posted 1 year ago #

    used for all pages which are grandchildren

    does this mean, the page starts on the grandchildren level?

    in this case, do you want to show all the relatives of the same generation?

    you probably need to get the grandparent page id first, and use the code with the grandparent page id.

    instead of this line:

    $postid = $post->ID;

    try and use:

    if($post->post_parent) {
    $postid = array_pop(get_post_ancestors($post->ID));
    } else { $postid = $post->ID; }

    http://codex.wordpress.org/Function_Reference/get_post_ancestors

  10. baga
    Member
    Posted 1 year ago #

    Thank you very much for your help, this solution is almost working.

    Yes, the page starts on the granchildren level, but I would like to show all grandchildren, even if they're not from the same generation.

    For example, if I'm on the page "granchild 2.1", right now I can only display "granchild 2.2" and "granchild 2.3"; I'd like to display all granchildren like "granchild 1.3" and "granchild 3.1" as well.

  11. alchymyth
    The Sweeper
    Posted 1 year ago #

    you need to get a list of all top level page ids, and run the code with these.

    this should show all grandchild level pages for the whole site - regardless on which individual grandchild page you are:

    http://pastebin.com/drsxdBGY

  12. baga
    Member
    Posted 1 year ago #

    That works perfectly, thanks!

    Just one more question, I tried to add:

    'orderby' => 'rand',
    'showposts' => 5,

    in order to limit 5 granchildren and have them displayed in a random order but didn't work. Any suggestion?

  13. alchymyth
    The Sweeper
    Posted 1 year ago #

    in order to limit 5 granchildren

    out of all grandchildren?
    or 5 per child?

    the following is for 5 per child:

    (that is the code starting at line 19):

    $args = array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'orderby' => 'rand',
    'posts_per_page' => 5,
    'post_parent' => $child_page->ID
    );

    (i don't have enough data to test it)

  14. baga
    Member
    Posted 1 year ago #

    Yes I mean to limit 5 granchildren out of all granchildren.

    Anyway i tried the code above starting al line 19, it still displays all granchildren (no limit) and no random order.

  15. alchymyth
    The Sweeper
    Posted 1 year ago #

    last try with this code:

    how to show 5 random pages on grandchild page level:

    http://pastebin.com/znYLxFcY

    i have the feeling that there has to be some simpler method...

  16. baga
    Member
    Posted 1 year ago #

    THANK YOU VERY MUCH, it now works 100% perfect :)

  17. alchymyth
    The Sweeper
    Posted 1 year ago #

    great ;-)

    here a new version - much less queries, probably faster;

    if you like to test it:

    http://pastebin.com/dUhnxZ25

    (make a backup of the working template)

  18. baga
    Member
    Posted 1 year ago #

    Yes the new version works perfectly as well and it is lighter :-)

    Thanks again, you gave me a great help!!!

Topic Closed

This topic has been closed to new replies.

About this Topic