WordPress.org

Forums

[resolved] Complex loop question (Show child pages of page on 2 columns) (4 posts)

  1. 3stripe
    Member
    Posted 9 years ago #

    I'm using the loop below to include all the child pages of the current static page:

    <?php $posts = @$wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status='static' AND post_parent='$pageID' ORDER BY post_title ASC"); if($posts) : foreach($posts as $post) : start_wp(); ?>

    I'd love to split this automatically into 2 columns, with half of the child pages outputted to one column, and the remained to the other. (even if there were an odd number of them)

    Can anyone advise on multiple loops? (I'm very stuck)

    Thank you for your time.

  2. 3stripe
    Member
    Posted 9 years ago #

    In fact, I'm using some new code now, which {nearly} works... but the if statement at the end to detect the halfway point in the loop is not working...

    $parent_title = $page->post_title;
    // See if there are any subpages of this page
    $subpages = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = '$parent_id'");
    if( $subpages > 0 ) {
    // Grab the HTML formatted list of subpages
    $junk .= wp_list_pages("child_of=$parent_id&depth=$depth&sort_column=menu_order&echo=".(!$return)."&title_li=0");
    //Break them into an array of individual lines
    $lines = explode("n", $junk);
    $postcount = 0;
    echo('<div id="leftcol">');
    // Step through the lines and ...
    foreach ($lines as $value) {
    echo $postcount;
    // Isolate just the post-slug-title
    $temp = explode("/\" title=\"", $value);
    $temptwo = explode("/", $temp[0]);
    $size = count($temptwo)-1;
    $getme = trim($temptwo[$size]);
    $name_or_id = "post_name = '$getme' AND";
    // Here we grab post data from the database
    $post = $wpdb->get_row("SELECT * FROM $table WHERE $name_or_id post_date <= '$now' AND ($query_suffix) ORDER BY post_date DESC LIMIT 1");
    // Display the image from the custom meta field
    $currentpostid = $post->ID;
    $postmeta = get_post_meta($currentpostid, 'prepic', true);
    echo("<img class='mugshot' src='".$blogurl."http://www.e-tv-interactive.com/new/".$postmeta."' />");
    // And grab and display the actual title while we are in the neighborhood
    $grab = explode("\"", $temp[1]);
    echo("<h3>$grab[0]</h3>");
    // And echo the post content
    echo("<p>");
    echo($post->post_content);
    echo("</p>");
    echo("n");
    // Once we are halfway through the child pages, start the second column
    if ($postcount == $size/2) {
    echo('</div>');
    echo('<div id="rightcol">');
    $postcount++;
    }
    }
    echo('</div>');
    }

    Any ideas much appreciated :)

  3. 3stripe
    Member
    Posted 9 years ago #

    Fixed :)

    if ($postcount == $size/2) {
    echo('</div>');
    echo('<div id="rightcol">');
    }
    $postcount++;
    }

  4. mores
    Member
    Posted 8 years ago #

    hey 3stripe,

    i'd like to pick your brain :)

    could you explain to me, where i need to put the code you mentioned in your first post

    <?php $posts = @$wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status='static' AND post_parent='$pageID' ORDER BY post_title ASC"); if($posts) : foreach($posts as $post) : start_wp(); ?>

    my page.php looks like this:

    <?php get_header(); ?>

    <!-- content ................................. -->
    <div id="main">

    <?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>

    <h2><?php the_title(); ?></h2>

    <?php the_content(); ?>

    <?php endwhile; ?>

    <?php endif; ?>

    </div> <!-- /content -->

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

    when i put your code before the "endif" line, i get a "parse error: unexpected T_ENDWHILE ..."

    any ideas what i'm doing wrong ?

    thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic