Display posts from a category on a page

The need for this arose when I wanted to have a menu item called “news” on a WordPress site used as a CMS. We all know that it’s not easy to integrate a post category into your pages menu. What you could do is add a page called “news” and use a plugin to redirect the page to the category you want to show.

There is just one problem. As soon as you are redirected to a category, the built-in conditional menu highlighting doesn’t work anymore. Meaning that even if you were reading “news”, the menu item “news” in you menu wouldn’t be highlighted.

So this code displays the posts from your news-category but on a page (page.php or your own custom page template) and you can now enjoy the benefits of menu highlighting.

Code Snippet:

<?php
$displayposts = new WP_Query();
  //get posts from your news category
  $displayposts->query('cat=5');
while ($displayposts->have_posts()) : $displayposts->the_post();
?>
<?php the_content('read more...'); ?>
<?php edit_post_link('edit', '', ''); ?>
<?php endwhile; ?>

Posted in Posts