Pages that Show Their Title and Text Plus a Category’s Posts
-
I’m looking for an elegant and flexible approach to a simple need:
I have a series of merchants to display in various categories such as Retail, Restaurant, Entertainment, etc. There are pages to match each category with some intro text entered in the body field. They then show in the site’s navigation.
To fill the above pages dynamically, I want to create a post for each merchant that has the name, description, image and so on with a relevant category selected.
DESIRED DISPLAY
With these two Pages and Posts elements set up, I want the merchant pages to show 3 things:
1) Page title at the top (e.g., “Retail” or “Restaurant”, etc.)
2) Brief intro text
3) Beneath the above two, a Category loop that displays all posts that match the current page’s title.
In other words, The Retail page would have a “Retail” title, a short intro about the retail shops, and then a loop of every Post that has the Retail category checkbox selected.
BONUS) I want to use the WP SNAP! plugin that creates a alphanumeric horizontal list at the top of the Category loop containing just the posts that match the current category. It requires just one php hook within the loop like so:
<?php if (function_exists('wp_snap')) { echo wp_snap('category_name=retail&child=true&firstload=all'); } ?>
SOLUTION…?
So, I assume I have to create a new template page (such as page-merchants.php) that contains some sort of get_posts or query_posts directive to grab the posts for given category and inject it into a matching page. Then each page would be directed to use this new template page file.
Ideally, this would be just one template page file using multiple if/else statements that looks for a page’s slug and then injects the matching category posts (as opposed to a custom page template file for each category).
CUMBERSOME SUCCESS(-ish)
I was able to get almost what I needed with the cumbersome approach of creating multiple page templates for EACH page category and then putting the following code in each (using the Retail example again):
<?php query_posts('category_name= retail&showposts=1000&orderby=title&order=asc'); ?> <?php if (function_exists('wp_snap')) { echo wp_snap('category_name=retail&child=true&firstload=all'); } ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Once I set this I could get a nice loop of all the posts that have the Retail category checked. Unfortunately, this approach has 3 limitations:
1) It kills the Page’s title and body text so that ONLY the titles and body content of the category Posts display. I have to hardcode the Page’s title and intro text into the individual template files.
2) It requires I make a bunch of nearly identical template files that would require manual updating and management should anything change.
3) Also, concerning proper SEO, I can hardcode a h2 header for the Page title within the template, but can’t find a way to make the category Post titles use an h3 header. They all display in h2 tags too.
SINGLE TEMPLATE PAGE TRY
As mentioned above, I’d like to get this category rendering code into a single template file that all of the Pages utilize (Retail, Restaurant, Entertainment, etc.). I found this simple code idea but it doesn’t even work:
`<?php
if ($page_id==’retail’){
query_posts(‘category_name=retail&showposts=10&orderby=title&order=asc’);
};
if($page_id==’restaurant’){
query_posts(‘category_name=restaurant&showposts=10&orderby=title&order=asc’);
};
if($page_id==’entertainment’){
query_posts(‘category_name= entertainment&showposts=10&orderby=title&order=asc’);
}
?>Seems like it’s missing else bits. Anyhow, this still would probably kill the Page’s title and intro body text so that I’d have to hardcode those somehow.
- The topic ‘Pages that Show Their Title and Text Plus a Category’s Posts’ is closed to new replies.