• Resolved finestimaginary

    (@finestimaginary)


    Hi guys,

    Currently using flutter, but that’s not the problem here.
    Trying to get some info from the children of a page parent to appear in the page parent’s content. Using this code atm but it’s not working :

    <? elseif(is_page('people')) // if page is People
    :?>
    
    <?php
        $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
        $count = 0;
        foreach($pages as $page)
        {
            $content = $page->post_content;
            if(!$content)
                continue;
            if($count >= 2)
                break;
            $count++;
            $content = apply_filters('the_content', $content);
        ?>
            <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
        }
    ?>	
    
    <? else: ?>

    I have other conditionals around it thus the elseifs and else. Help?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter finestimaginary

    (@finestimaginary)

    Alrighty, I figured it out ??

    <?
    $pages = $wpdb->get_results("
    	SELECT * from $wpdb->posts
    	WHERE post_type='page'
    	AND post_parent=$post->ID
    	ORDER BY post_title
    	");
    //the above checks the database for posts that are actually pages, it then (I think?) put these into the pages variable, and orders
    //them by title.	
    
    foreach($pages as $page) : // christ, I don't know php... so I guess this makes teh variable of page act as pages but singularly?
    	$profile_image = get_post_meta($page->ID, 'profile_image', $single = true); // this bit is to pull any meta from the child page
    	//in this case it's the custom field with the profile_image key, turning to to the variable profile image
    endforeach;
    foreach($pages as $page) :
    	//echo '<h2><a href="' .  get_permalink($page->ID) .  '" title="' . $page->post_title . '">' . $page->post_title .  '</a></h2>'; //this would, if uncommented, show the page title.
    	echo '<p><a href="' .  get_permalink($page->ID) .  '" title="' . $page->post_title . '"><img src="/wp-content/plugins/fresh-page/files_flutter/' . $profile_image . '" /></a></p>';
    	// using flutter, so I've pulled the profile image from the page, linked it to the folder that it resides in and linked around it to the actual child page.
    endforeach;
    ?>

    The code has been pinched from various places, I’m only a beginner in php so I’m quite proud of figuring this out!

    Thread Starter finestimaginary

    (@finestimaginary)

    hum ok so the images don’t work… it’s just pulling the same meta in from the first page

    Thread Starter finestimaginary

    (@finestimaginary)

    fixed it! Works like a charm now :

    <?
    $pages = $wpdb->get_results("
    	SELECT * from $wpdb->posts
    	WHERE post_type='page'
    	AND post_parent=$post->ID
    	ORDER BY post_title
    	");
    //the above checks the database for posts that are actually pages, it then (I think?) put these into the pages variable, and orders
    //them by title.	
    
    foreach($pages as $page) :
    	$profile_image = get_post_meta($page->ID, 'profile_image', $single = true);
    	// this bit is to pull any meta from the child page
    	//in this case it's the custom field with the profile_image key, turning to to the variable profile_image
    	//echo '<h2><a href="' .  get_permalink($page->ID) .  '" title="' . $page->post_title . '">' . $page->post_title .  '</a></h2>'; //this would, if uncommented, show the page title.
    	echo '<p><a href="' .  get_permalink($page->ID) .  '" title="' . $page->post_title . '"><img src="/wp-content/plugins/fresh-page/files_flutter/' . $profile_image . '" /></a></p>';
    	// using flutter, so I've pulled the profile image from the page, linked it to the folder that it resides in and linked around it to the actual child page.
    endforeach;
    ?>
    Thread Starter finestimaginary

    (@finestimaginary)

    is there an easier way to do this query posts?

    mendezki

    (@mendezki)

    I like to do it like this, using setup_postdata for easy access to post stuff like the_title. Just cobbled together too, but still:

    <?php
    $current_page = $post->ID;
    $querystr = "
    	SELECT * from $wpdb->posts
    	WHERE post_type = 'page'
    	AND post_parent = $current_page
    	";
    
    $child_pages = $wpdb->get_results($querystr, OBJECT);
    if ($child_pages):
    foreach($child_pages as $post) :
    setup_postdata($post); ?>
    <?php the_title(); ?>
    <?php endforeach; ?>
    <?php else : ?>
    // Do stuff when no child page available
    <?php endif; ?>
    <?php rewind_posts(); ?>

    I’ve added rewind_posts so this stuff doesn’t mess up my original query when on an archive page and such.

    Thread Starter finestimaginary

    (@finestimaginary)

    cool, I’m not sure if I need rewind posts? but I’m not sure… maybe i should put it in a one f the temp query things though? Would it affect searchs and the like?

    mendezki

    (@mendezki)

    I had some troubles with an additional query and the ‘edit post’ link, where Kafkaesqui pointed me to rewind_posts.

    Not sure what you mean by affecting search though.

    I found this posting by mistake and it solved a problem I was having.

    Thanks

    https://www.ads-software.com/support/topic/246014

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Showing data from child pages in page parent’ is closed to new replies.