我已经连续7天努力完善代码,在有孩子的页面上生成一个子菜单。因为它太复杂了,我想我应该把它写在我的索引中。php文件,然后在工作后将其迁移到小部件插件。它现在可以完美地工作,但当我将它移到我的插件时,它无法正确识别页面级别(父/子/孙)。我已经测试了插件,以确保我没有做任何设置错误。它正常工作。
下面是在我的索引中正常工作的代码。php。我尝试在我的<aside>
插件的标签(不包括jquery),我会得到一级+二级页面,其中我应该有二级,一级页面中我应该有三级:
<aside class="sidebar">
<div class="submenu">
<?php
if(count(get_post_ancestors($post->ID)) == 1 ) {
echo \'<ul>\';
$args = array(
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'post_parent\' => $post->post_parent,
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
$child = get_pages(\'child_of=\'.$post->ID);
if( count( $child ) != 0 ) : ?>
<li class="has-children"><a href="#<?php the_ID(); ?>" class="list-group-item" data-toggle="collapse"><?php the_title(); ?><i class="glyphicon glyphicon-chevron-right"></i></a>
<?php $children = wp_list_pages( \'title_li=&child_of=\'.$post->ID.\'&echo=0&depth=1\' );
if ( $children) : ?>
<ul class="children collapse" id="<?php the_ID(); ?>">
<li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?> ><a href="<?php the_permalink(); ?>">Overview</a></li>
<?php echo $children; ?>
</ul>
</li>
<?php endif; ?>
<?php else : ?>
<li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endif;
}
echo \'</ul>\';
}
elseif (count(get_post_ancestors($post->ID)) == 2 ) {
echo \'<ul>\';
$args = array(
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'post_parent\' => get_post( $post->post_parent )->post_parent,
\'depth\' => 1,
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
$child = get_pages(\'child_of=\'.$post->ID);
if( count( $child ) != 0 ) : ?>
<li class="has-children"><a href="#<?php the_ID(); ?>" class="list-group-item" data-toggle="collapse"><?php the_title(); ?><i class="glyphicon glyphicon-chevron-right"></i></a>
<?php $children = wp_list_pages( \'title_li=&child_of=\'.$post->ID.\'&echo=0&depth=1\' );
if ( $children) : ?>
<ul class="children collapse" id="<?php the_ID(); ?>">
<li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?> ><a href="<?php the_permalink(); ?>">Overview</a></li>
<?php echo $children; ?>
</ul>
<?php endif; ?>
</li>
<?php else : ?>
<li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endif;
}
echo \'</ul>\';
}
elseif (get_pages(\'child_of=\'.$post->ID) ) {
echo \'<ul>\';
$args = array(
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'post_parent\' => $post->ID,
\'depth\' => 1,
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
$child = get_pages(\'child_of=\'.$post->ID);
if( count( $child ) != 0 ) : ?>
<li class="has-children"><a href="#<?php the_ID(); ?>" class="list-group-item" data-toggle="collapse"><?php the_title(); ?><i class="glyphicon glyphicon-chevron-right"></i></a>
<?php $children = wp_list_pages( \'title_li=&child_of=\'.$post->ID.\'&echo=0&depth=1\' );
if ( $children) : ?>
<ul class="children collapse" id="<?php the_ID(); ?>">
<li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?>><a href="<?php the_permalink(); ?>">Overview</a></li>
<?php echo $children; ?>
</ul>
<?php endif; ?>
</li>
<?php else : ?>
<li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endif;
}
echo \'</ul>\';
}
wp_reset_postdata();
?>
<script type="text/javascript">
var $ = jQuery;
$(document).ready(function(){
$(function() {
$(\'.list-group-item\').on(\'click\', function() {
$(\'.glyphicon\', this)
.toggleClass(\'glyphicon-chevron-right\')
.toggleClass(\'glyphicon-chevron-down\');
});
});
if ( $(\'.children li\').hasClass(\'current_page_item\') ) {
$(\'.list-group-item .glyphicon\', this)
.toggleClass(\'glyphicon-chevron-right\')
.toggleClass(\'glyphicon-chevron-down\');
$(\'.children\', this)
.addClass(\'in\');
};
});
</script>
</div>
</aside>