WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Retrieve specific child page with query_posts($query) (3 posts)

  1. elC0mmen
    Member
    Posted 5 years ago #

    Hello everybody!

    I am using WP as a CMS. I have a number pages (the names of student dorms), lets say 'dorm1', dorm2', etc. and each of them has childres pages with THE SAME names/slugs, eg. 'dorm1/tutoren', 'dorm1/sonstiges', 'dorm2/tutoren', 'dorm2/sonstiges' , etc.

    I want that the dorm pages 'dorm1' show the 'tutoren' child page, so I tried this, but it doesn't work.

    $post_id = get_post($post->ID);
    $slug = $post_id->post_name;
    $query = 'pagename='.$slug.'/tutoren';
    query_posts($query);

    Where is the error?
    Also, I want this to happen only on "mother" pages, not the children. I tried this:

    if($post->post_parent) {
    //the above code
    }

    Many thanks for you help!

  2. MichaelH
    Member
    Posted 5 years ago #

    There's an example in Function_Reference/get_pages that you might find useful.

  3. elC0mmen
    Member
    Posted 5 years ago #

    Thanks A LOT! That helped! This is my solution!

    <?php //
    $pages = get_pages('child_of='.$post->ID);
    foreach($pages as $page)
    	{
    	$content = $page->post_content;
    	if($page->post_title=="Tutoren")
    		{
    		$content = apply_filters('the_content', $content);
    		}
    	else continue;
    ?>
    <h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    <div class="entry"><?php echo $content ?></div>
    <?php    }   ?>

Topic Closed

This topic has been closed to new replies.

About this Topic