我目前正在开发一个WordPress网站,在大多数页面上都有一个侧边栏,这个侧边栏应该显示当前正在查看的父页面下的所有子页面的小导航,以及两篇最新的帖子。
目前,我在侧边栏上显示链接时遇到了问题;它们只显示在网站的主父页面上,而不是我需要的子页面上。
我创建了以下内容:
<?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>
 我还从这里的另一个问题中偶然发现了这段代码;它执行相同的操作(仅显示在父页面上),但也显示指向实际父页面的链接。
<?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 
        }
        ?>
 非常感谢您的帮助。非常感谢。
