WordPress.org

Support

Support » How-To and Troubleshooting » wp_list_pages, sub navigation, and the blog page

wp_list_pages, sub navigation, and the blog page

  • Hello all!
    I’m using the following code to list child pages as a sub navigation in my sidebar.

    <?php /* Creates a menu for pages beneath the level of the current page */
    if (is_page() and ($notfound != '1')) {
    $current_page = $post->ID;
    while($current_page) {
    $page_query = $wpdb->get_row("SELECT ID, post_title, post_status, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    $current_page = $page_query->post_parent;
    }
    $parent_id = $page_query->ID;
    $parent_title = $page_query->post_title;
    
    if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?>
    
    <ol>
    <?php wp_list_pages('sort_column=menu_order&title_li=&child_of='. $parent_id); ?>
    </ol>
    <?php } } ?>

    Problem is, it doesn’t work on the page I have designated to be the Posts page. In the hierarchy, this page is a child of a parent page. To see what I’m talking about, this is the site I’m working on: http://cjdschool.org/connect/news. That link is specifically the one I’m having trouble with.

    From searching around, I’ve pretty much gathered that WordPress doesn’t let index.php be a child/have a child? Is that right? Meaning that the reason the list isn’t showing up is because that page is designated as not having any parents or children.

    If that’s right, the only thing I can think of doing is making a new template, deactivating that page from being the Posts page, and adding in some code to pull posts anyway? Would that work? If so, does anyone have any idea of that that code could be?

    Many, many thanks in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • Well.
    I found the quick & dirty solution:

    <?php
    	$args = array( 'numberposts' => 3 );
    	$lastposts = get_posts( $args );
    	foreach($lastposts as $post) : setup_postdata($post); ?>
    		<div class="post">
    			<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    			<?php twentyten_posted_on(); ?>
    			<?php the_content(); ?>
    			<?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?>
    		</div><!--END .post-->
    	<?php endforeach; ?>

    That code in a template that’s not designated as a Posts page.

    If anyone can think of something cleaner, I would still love to hear it. Thanks!

    $current_page = $post->ID;

    does not work on the posts page;

    the ID of the static page set as posts page would be:

    if( !is_page() && is_home() ) $current_page = get_option('page_for_posts');

    http://www.blog.highub.com/cms/wordpress/wordpress-front-page-posts-page-id/

    http://codex.wordpress.org/Conditional_Tags#The_Main_Page

    ps: you need to integrate that somehow into the existing if statement of your code.

    I see what you mean. I wouldn’t have even known to look into that.

    I tried this:
    [Code moderated as per the Forum Rules. Please use the pastebin]

    but no luck so far. I could be getting the coding wrong. Is it okay to have the two if statements together like that? Doesn’t seem right to me.

    Thank you so much, by the way, for helping out.

    try to replace this section:

    if (is_page() and ($notfound != '1')) {
    $current_page = $post->ID;
    while($current_page) {

    with:

    $current_page = '';
    if (is_page() and ($notfound != '1')) { $current_page = $post->ID; }
    if( !is_page() && is_home() and ($notfound != '1') ) { $current_page = get_option('page_for_posts'); }
    if( $current_page ) {
    while($current_page) {

    (might depend on the and ($notfound != '1') part which I don’t understand out of context.)

    Haha, I don’t understand what it means either, but you did it! It’s solved!

    The final code turned out to be:

    <?php
    	$current_page = '';
    	if (is_page() and ($notfound != '1')) { $current_page = $post->ID; }
    	if( !is_page() && is_home() and ($notfound != '1') ) { $current_page = get_option('page_for_posts'); }
    	if( $current_page ) {
    		while($current_page) {
    		$page_query = $wpdb->get_row("SELECT ID, post_title, post_status, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    		$current_page = $page_query->post_parent;
    	}
    	$parent_id = $page_query->ID;
    	$parent_title = $page_query->post_title;
    
    	if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?>
    
    	<ol>
    		<?php wp_list_pages('sort_column=menu_order&title_li=&child_of='. $parent_id); ?>
    	</ol>
    <?php } } ?>

    For other people with the same problem, this code makes a sub navigation that shows up on the posts page and shows a parent’s child pages while on a child’s child pages.

    You’re the best, alchymyth, thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_list_pages, sub navigation, and the blog page’ is closed to new replies.
Skip to toolbar