Getting different posts for different pages in WordPress

I’m trying to figure out the best method to display posts of different categories on different pages specific for those categories.

The scenario:

5 Pages (links on menu): Home, About, Apples, Oranges, Contact
The posts will be about fruits which will be either of category apple, or category orange.
The posts of category apple should be displayed only on the page called apples. And the posts of category orange should be displayed only on the page called oranges.

I could achieve this by using the category or archive template but there are 2 reasons why I want to use pages created in the admin panel instead:

  1. The end user should be able to add an introductory paragraph to describe the fruit at the beginning of the page which is then followed be the list of posts.
  2. I prefer to have a url that looks like this http://www.website.com/apples/ than one that looks like this http://www.website.com/category/apples/

Another way to achieve this is to create a page template for each fruit with a query to call its specific category. I actually have it working in this way at the moment. So I have a page-apples.php with a query to get and display posts of category apple. And a page-oranges.php with a query to get and display posts of category orange.

I’m trying to figure out now if there is a more efficient way to do it. Instead of using 3 page templates (for apples, for oranges, and for the other regular pages) to have just one page template that can be reused for each of the 3 types of pages that I have.

I’m using underscores starter theme which uses different template parts to display specific content for different page templates. I thought of using conditionals in page.php to do something like this:

if page is apples -> get posts of category apple -> get template part for content

else if page is oranges -> get posts of category orange -> get template part for content

else get template part for content

So there would be just one page template which would check if the page was either apples, oranges, or one of the other 3 standard pages, and then display the appropriate content based on what page it was.

Would this way actually be more efficient? Would more conditions in a single page result in slower load time due to more queries being made to the database? Maybe this way would be more simple structurally but less efficient in terms of performance? Or is it even less efficient using multiple page templates?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.