[wp-hackers] Custom Page Template - can't see the forest through the trees

John Bloch jbloch at olympianetworks.com
Fri Apr 2 15:40:52 UTC 2010


Mike,

Are you running it in a loop?

Either way, I always use get_posts instead of starting a separate query. It
has basically all the same options. For that, you'd use

<?php
$new_posts = get_posts( 'category=23' );
foreach( $new_posts as $post ):
setup_postdata( $post );

endforeach;
?>

5 is the default number of posts, so you wouldn't need to declare it in
get_posts, but the argument is numberposts if you want to do it anyway.
setup_postdata gives you all the loop functions, like the_title() or
the_permalink(). For more information, the codex page is pretty good:
http://codex.wordpress.org/Template_Tags/get_posts

John P. Bloch


On Fri, Apr 2, 2010 at 10:21 AM, Mike Walsh <mike_walsh at mindspring.com>wrote:

> I am stuck doing something I think should be rather simple but I can't seem
> to see why it isn't working but suspect it is my lack of knowledge of the
> loop.
>
>
>
> I want to create a page template that is essentially the same as the main
> index.php except that it will only shows posts from a single category.  I
> can't get it to work correctly, the only thing I can do is get it to
> display
> the single most recent post from the category.
>
>
>
> I referred to this page on the Codex:
>
>
>
> http://codex.wordpress.org/Template_Tags/query_posts#Category_Parameters
>
>
>
> I thought I could simply do this outside my loop:
>
>
>
>                                <?php
> query_posts('cat=23&posts_per_page=5');?>
>
>
>
> I currently have three posts with the category ID of 23 and all three
> appear
> when clicking on the category link.  If I examine  $posts after running the
> above query_posts() it only has one post, the most recent one.
>
>
>
> Is there something special about using the loop on a page template that I
> don't understand?
>
>
>
> Thanks,
>
>
>
> Mike
>
>
>
> --
>
> Mike Walsh - mike underscore walsh at mindspring dot com
>
>
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list