PHP:为什么我的代码可以在index.php中运行,但不能在小部件中运行?

时间:2016-08-12 作者:Heather

我已经连续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>

2 个回复
最合适的回答,由SO网友:Heather 整理而成

我通过添加global $post; 到顶部。

我在与用户合作时遇到了此解决方案sisir 关于故障排除。在尝试进行这些练习时,他建议我看到以下帖子:$post->ID not working

SO网友:Victor Savio Fernandes

这可能不是你想要的答案,但你能试着用一下吗PHP Code Widget 检查它是否有效?如果是这样的话,您的小部件生成代码可能有问题。如果没有,请尝试将其添加到侧栏。php而不是索引。然后再次检查结果。