排除子页面参数数组

时间:2011-08-25 作者:Infocentre

我有以下代码,需要它来排除页面ID的子页面;

<?php
      $args = array(
                    \'post_type\' => \'page\',
                    \'post_status\' => \'publish\',
                    \'numberposts\' => -1,
                    \'exclude\' => \'3984,1939,6006,28,2784\',
                    \'depth\' => 1
                );
                     $postslist = get_posts($args);
                     foreach ($postslist as $post) :
                        setup_postdata($post);
                     ?>
                <li>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                        <?php if ( has_post_thumbnail() ) { the_post_thumbnail(\'portrait\');} else { echo \'<img src="#" alt="Something Went Wrong?" title="Sorry - Something went wrong?" />\';}?>    
                    </a>
                        <span><?php meta(\'issue-number\'); ?></span>
                </li>

     <?php endforeach; ?>
“深度”似乎没有任何影响。。。

谢谢

2 个回复
SO网友:danielwiener

get\\u posts没有深度参数。看见http://codex.wordpress.org/Template_Tags/get_posts

您需要检查页面是否有父级,如果没有父级,则需要显示它。

进入foreach循环后,添加if语句

<?php if( !$post->parent ): ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <?php if ( has_post_thumbnail() ) { the_post_thumbnail(\'portrait\');} else { echo \'<img src="#" alt="Something Went Wrong?" title="Sorry - Something went wrong?" />\';}?></a>
     <span><?php meta(\'issue-number\'); ?></span>
     </li>
<?php endif: ?>

SO网友:Infocentre

                <?php
      $args = array(
                    \'post_type\' => \'page\',
                    \'post_status\' => \'publish\',
                    \'numberposts\' => -1,
                    \'exclude\' => \'3984,1939,6006,28,2784\',
                    \'exclude_tree\' => \'1939\',
                    \'sort_order\' => \'desc\'
                );
                     $postslist = get_pages($args);
                     foreach ($postslist as $post) :
                        setup_postdata($post);

                     ?>
                <li>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                        <?php if ( has_post_thumbnail() ) { the_post_thumbnail(\'portrait\');} else { echo \'<img src="#" alt="Something Went Wrong?" title="Sorry - Something went wrong?" />\';}?>    
                    </a>
                        <span><?php meta(\'issue-number\'); ?></span>
                </li>

     <?php endforeach; ?>
多亏了凤凰城,我所需要做的就是换成get\\U页面。这为我提供了额外的选择

结束

相关推荐

按菜单顺序而不是顺序对“Get_Pages”进行排序

我正在使用这段代码生成一个充满项目的页面,但它们不会按菜单顺序排序,你知道为什么吗? <?php $pages = get_pages(array(\'child_of\', \'menu_order\')); ?>