• Resolved gearu

    (@gearu)


    I need some help to get the child id of a page. I am grabbing the content off the child page, and displaying the content in a text box on the parent page.

    I have achieved this previously by just sticking in the page ID’s in the code – but i want a dynamice way to do this.

    I searched the codex and found get_page_children, which I think returns what I need – I just don’t know how to get the page ID’s out of this?

    Here is what i have so far:

    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
    $current_page_ID = $post->ID;
    $child_pages = get_page_children( $current_page_ID, $all_wp_pages ) 
    
    //need to get the child page ID from $child_pages...then add in below
    // echo getPageContent(ID_of_the_child_page);

    For the site I am creating I only have one child page, so i really am most interested in getting the ‘first’ child page ID.

    Can you help?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Here is what I think you want. I added ‘posts_per_page’ to the query. Otherwise, you only get back the number set in Admin->Settings->Reading.

    I also assume that this is used in the Loop. Otherwise, $post->ID will be empty.

    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => -1));
    $current_page_ID = $post->ID;
    $child_pages = get_page_children( $current_page_ID, $all_wp_pages ) 
    
    // Get the ID from the first $child_pages entry
    $child_id = $child_pages[0]->ID;
    // echo getPageContent($child_id);
    Thread Starter gearu

    (@gearu)

    Thanks vtxyzzy.
    This was what I was trying to do, and you way look like what i wanted! I eventually managed to get what I needed by doing this:

    $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    $count = 0;
    foreach($pages as $page)
    {
    echo getPageContent($page->ID);
    }

    getPageContent is a function which returns the content of a page with formatting maintained:

    if(!function_exists('getPageContent'))
    	{
    		function getPageContent($pageId)
    		{
    			if(!is_numeric($pageId))
    			{
    				return;
    			}
    			global $wpdb;
    			$sql_query = 'SELECT DISTINCT * FROM ' . $wpdb->posts .
    			' WHERE ' . $wpdb->posts . '.ID=' . $pageId;
    			$posts = $wpdb->get_results($sql_query);
    			if(!empty($posts))
    			{
    				foreach($posts as $post)
    				{
    					return nl2br($post->post_content);
    				}
    			}
    		}
    	}

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get the ID of Child Pages?’ is closed to new replies.