0

I am currently developing a WordPress website that has a sidebar on most of the pages, this sidebar is supposed to display a small navigation of any child pages that are under the current parent page that is being viewed, along with two of the most recent posts.

I am having trouble with having the links to display on the sidebar, currently; they are only displaying on the main parent pages of the site and not on the child pages which is what I need.

I have created the following:

<?php

$args = array(
    'post_type'      => 'page',
    'posts_per_page' => 10,
    'post_parent'    => $post->ID,
    'order'          => 'ASC',
    'orderby'        => 'menu_order'
 );


$parent = new WP_Query( $args );

if ( $parent->have_posts() ) : ?>

<section class="links border shadow">

    <ul>

        <?php while ( $parent->have_posts() ) : $parent->the_post(); ?>


        <li class="child-title">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>


            <?php

                    $args2 = array(
                        'post_type'      => 'page',
                        'posts_per_page' => 10,
                        'post_parent'    => $post->ID,
                        'order'          => 'ASC',
                        'orderby'        => 'menu_order'
                     );


                    $parent2 = new WP_Query( $args2 );

                    if ( $parent2->have_posts() ) : ?>

            <?php while ( $parent2->have_posts() ) : $parent2->the_post(); ?>

            <div id="sub-<?php the_ID(); ?>" class="child-sub">

                <p class="sub-title"><a href="<?php the_permalink(); ?>"
                        title="<?php the_title(); ?>"><?php the_title(); ?></a></p>

            </div>

            <?php endwhile; ?>
        </li>

        <?php endif; wp_reset_postdata(); ?>

        <?php endwhile; ?>

    </ul>

</section>

I also stumbled across this snippet of code from another question on here; which does the same thing (only displays on parent pages), but also displays a link to the actual parent page too.

<?php
        if($post->post_parent){
            $children = get_pages("child_of=".$post->post_parent);
            $parent_title = get_the_title($post->post_parent);
            $link = get_permalink($post->post_parent);
        }
        else{
            $children = get_pages("child_of=".$post->ID);
            $parent_title = get_the_title($post->ID);
            $link = get_permalink($post->ID);
            $parent_page = $post->ID;
        }
        if ($children) {
        ?>
            <li <?php if( !empty($parent_page) && $parent_page==$post->ID){echo 'class="current-menu-item"';} ?>><a href="<?php echo $link; ?>"><?php echo $parent_title;?></a></li>
            <?php 
                foreach( $children as $post ) : setup_postdata($post); 
            ?>
                    <li <?php if(is_page($post->ID)){echo 'class="current-menu-item"';} ?>>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
            <?php 
                endforeach;
            ?>
        <?php 
        }
        ?>

Any help is appreciated. Thank you.

Current Parent Page (How I want all pages to look)

0

The following fixed the issue for me:

<?php
switch ($isBlogsPostPage){
    case true:
        $parentID=get_correct_id($post, $isBlogsPostPage);
        break;
    default:
        $parentID=get_correct_id($post);
}


$args = array(
    'post_type'      => 'page',
    'posts_per_page' => 10,
    'post_parent'    => $parentID,
    'orderby'        => 'menu_order'
 );
$parent = new WP_Query( $args );
if ( true ) : ?>

    <section class="links border shadow">
        <ul>
            <?php while ( $parent->have_posts() ) : $parent->the_post();?>

                    <li class="child-title <?php if ((is_page(get_the_ID())) || (get_the_ID()===(int) get_option( 'page_for_posts' ) && $isBlogsPostPage)) echo ('active'); ?>">
                        <a  href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                    </li>
                    <?php /*endif;*/ wp_reset_postdata(); ?>    
            <?php endwhile; ?>
        </ul>
    </section>
<?php endif; wp_reset_postdata(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.