WordPress.org

Ready to get started?Download WordPress

Forums

Have a page template that includes posts? (5 posts)

  1. Mr_Cynical
    Member
    Posted 6 years ago #

    Is it possible to have a page template that can also fetch posts? I know that's not the clearest explanation. What I want to have is a page template that will display the page's title and content, but can also then fetch posts in a specified way (in my case it's fetching a specific category of posts)and display them underneath. Is this possible?

  2. kwbridge
    Member
    Posted 6 years ago #

    You can make a page template that works just like the main index page and then add a query for that particular category so only those posts show.

    For example, if you make a new template using the code from you main index template you can add something like the following

    <?php query_posts('category_name=wordpress&showposts=5'); ?>

    right before the loop starts

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

  3. Mr_Cynical
    Member
    Posted 6 years ago #

    What I was wanting to do is have a page template which displays that page's content as normal, and then under that content displays a particular category of posts. Could I do that essentially by reversing your code? Take the code from a normal 'single page' template, and then put the query_posts line under the loop?

  4. kwbridge
    Member
    Posted 6 years ago #

    The query posts has to come before a loop so you could make a second loop below the first one that only pulls the posts from the designated category.

  5. Mr_Cynical
    Member
    Posted 6 years ago #

    Would this be any more complicated than just copy-pasting the loop code so I have two (slightly different) copies of the same thing?

Topic Closed

This topic has been closed to new replies.

About this Topic