Restrict a Wordpress Template to show only one category

2k Views Asked by At

I tried everything found in the web, but i'm getting errors or that's not what I am searching for...

I have to make a PAGE in wordpress, to show ONLY the posts of the category with a certain ID ( in my case id=8 ) i tryed to edit the loop-xxxx.php .. the template file... everything but I get always a problem navigation system doesn't work. I mean... getting back to older posts won't work cause the output shows the last posts instead of older one.

The code I'm using in the loop or in the template file is:

<?php
query_posts('cat=8');
while (have_posts()) : the_post();
the_content();
endwhile;
?>

i tried inserting it before the

<?php while ( have_posts() ) : the_post(); ?>

in loop.php

or before the call of loop inside the index.php

please help me :\

2

There are 2 best solutions below

3
On

One solution is to use a custom WP_Query. In the custom page's TEMPLATE file, where ID is the id of the targeted category:

<?php $tmp_query = new WP_Query('cat=ID');
    while ( $tmp_query->have_posts() ) : $tmp_query->the_post();
        the_content();
    endwhile;
    wp_reset_postdata();
?>
0
On

Check this.

<?php query_posts($query_string . '&cat=8'); ?>

<?php if (have_posts()) : ?>
<optional> You can write here: "You are in category X". </optional>
<?php while (have_posts()) : the_post(); ?>

Good luck.