如何从WordPress的最近帖子的补充工具栏列表中排除类别?

时间:2011-07-14 17:01:53

标签: php wordpress

我正在尝试从侧边栏中列出的最近帖子中排除几个类别。这是我所拥有的,但它不起作用($ ex部分是我试图排除的地方)。任何建议表示赞赏:

<?php
        $latest = get_posts('numberposts=7');
        $i = 0;
        $ex = "65,86";
        ?>
        <?php foreach ($latest as $latest_post): $i++; ?>
            <li <?php if ($i === 1) echo 'id="most_recent"' ?>><a href="<?php echo get_permalink($latest_post->ID) ?>"><?php echo $latest_post->post_title ?></a></li>
        <?php endforeach ?>
    </ul>

</div>

1 个答案:

答案 0 :(得分:0)

我可能会为此使用自定义循环。类似下面的代码应该有效:

<!--Set up your query here.  In this example we're excluding cats 1, 2, 3 and displaying 5 posts-->
<?php query_posts( 'cat=-1,-2,-3&posts_per_page=5' ); ?>        

<!--Start the loop-->       
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<!--Your HTML and template tags here-->
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>

<?php endwhile; ?>

<?php else: ?>

<!-- Put something here in case there are not recent posts-->

<?php endif; wp_reset_query();?>
<!--Make sure you include the reset query function at the end here if you want other custom loops after this one-->

使用查询帖子的好处是,您可以使用wordpress

中提供的所有其他参数

http://codex.wordpress.org/Function_Reference/query_posts#Parameters