• I want to display child page preview with excerpt on a parent page. The child page will always have the same title, but the parent will be different.

    At the moment Im using this code:

    $page = get_page_by_title( 'Child Title' );
     $page_excerpt = $page->post_excerpt;
    <header class="wpb_wrapper">
        <h4 class="gp-element-title xcpt">
            <a href="<?php echo esc_url( get_permalink( $page ) ); ?>">
                <?php echo $page_data->post_title; ?>
            </a>
        </h4>
    </header>
        <div class="gp-entry-content xcpt page-layout"><?php echo $page_excerpt; ?><br/>
            <a href="<?php echo esc_url( get_permalink( $page ) ); ?>">Read More...</a>
        </div>

    This code sits in the parent page template and every time I create new parent page the parent link stays the same, but I want it take the slug from the new parent page. So for example now there is http://www.example.com/parent/child and when I create new parent page called “new parent” I want it to be http://www.example.com/new-parent/child.

    Also the excerpt doesnt work and I have to manually enter the text into metabox in admin, is there a way to display the excerpt dynamically? Thanks for any help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Here’s a nice shortcode that lists all children for a parent page:

    https://gist.github.com/sterndata/d737161f540b610a9f0e215572362fb5

    You don’t need any code in the template. Put this in functions.php and at the bottom of parent pages where you want the children listed, use

    [list_child_pages]

    Thread Starter searay

    (@searay)

    Thanks Steve, that works magic, with one exception, how can I choose which child pages to display and which not? It displays all of them(four in my case), and I want to have only two specific child pages. Also it doesnt display Read more link.

    UPDATE:
    I was able to add “Read more…” link, but still can’t figure out how to catch the desired child pages. Well, I could use some CSS tricks for that, but hoping for some php solution first.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    That’s what this code does. It lists them all. 🙂

    Moderator bcworkz

    (@bcworkz)

    You can get Steve’s code to return something other than all by changing the query arguments. Which ones you use depends on what you want returned. For example, if you know the post IDs you are after you can add a 'post__in' argument that is assigned an array of post IDs to return. The IDs must have the specified parent or nothing will be returned. The arguments available are quite diverse, you should find something that’ll work. See Class Reference/WP Query.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Parent and child pages in permalink’ is closed to new replies.