Posted by on October 5, 2011

There 2 siturations, first one, we only need to show certain category post, say category id 3. If the category id is 3, then the blog will show some posts under the category. We can use the following code:

<?php if ( is_category('3') ) : ?>
<li>
<h2>News</h2>
<ul>
<?php
$posts = get_posts('numberposts=10&category=3&orderby=post_title&order=ASC');
foreach($posts as $post) :
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endif ; ?>


Another situration is that we do not need to use if to check category id, we just show some posts under certain category direct, say categroy id 4, we can use the following code:

<h2>10 Posts under category 4</h2>
<ul>
<?php
$posts = get_posts(‘numberposts=10&category=4&orderby=post_title&order=DESC ‘);
foreach($posts as $post) :
?>
<li>
<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>

Posted in: WordPress