Question

I'm new to WordPress. I have my site (CMS) divided to several pages in a tree hierarchy. I'm trying to view posts in a specific category within a subpage. But for some reason event the simple "the loop":

<?php
if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_content();
   endwhile;
endif;
?>

Shows ONLY (!!) the page content and no posts at all ... How can i do that?

10x.

Was it helpful?

Solution

You need a call to query_posts() first, before your loop begins.

Example:

query_posts('showposts=5');

You can see the full documentation here:

http://codex.wordpress.org/Template_Tags/query_posts

I'm not entirely sure that you want your page content method call inside of the while loop, because it will be displayed over and over again. I suggest moving it to outside of the loop.

By the way, to get posts from a particular category, use:

<?php query_posts('category=category-name'); ?>

Where category-name is the name of the category itself. It might be the category's slug name instead, but I'd try that first.

OTHER TIPS

You can use this inside the loop to generate a (or a list) permalink of your latest post in one category. Change mycategoryname to your own category, and showposts to -1 to show all, or another number to show that number of posts.

<?php $my_query = new WP_Query('category_name=mycategoryname&showposts=1'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><?php endwhile; ?>

The basic idea of WP_QUERY is at Wordpress

as is The Loop, with examples

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top