Wordpress - How can I create my own template outside of the expected hierarchy of templates and feed a query to it?

StackOverflow https://stackoverflow.com/questions/5395316

BACKGROUND

I have used WordPress custom post types to create a newsletter section for my website. Newsletters consist of Articles (dm_article), which are grouped by the Issues taxonomy (dm_issues).

1) I have created an index of all of my newsletter Articles. I am using a template called taxonomy-dm_issues.php to loop within a selected Issue and display each Article's title, excerpt and a link to the full content, which is managed by single-dm_article.php. This is working great.

2) I would also like to create second taxonomy-based template for Issues. This is going to act as a print-friendly option: "print entire newsletter". I would like the template to loop through and display each Article title, excerpt, and long description. Some of the look and feel will also be different.

For #2, let's assume I've created a template called print-dm_issues.php. It currently looks identical to taxonomy-dm_issues.php, except it has the additional long description data for each Article and contains some different styling.

I want to setup this "print friendly" option without making the WordPress admin have to jump through any hoops when Issues and Articles are created. I also do not want to have to create a new template each time a new Issue is created.

CORE QUESTION:

What I am asking for may boil down to this: How can I create my own WordPress template outside of the expected hierarchy of templates and feed a query to it? Do note I am using the "month and name" common permalink structure, so I'll have to muck with my htaccess.

ALTERNATIVES:

1) My fallback is to have taxonomy-dm_issues.php contain the information for both variations and use style to handle the different view states. I know how to do this. But, I'd rather not do this for sake of load times.

2) Using Ajax to fetch all of the Article long descriptions (the_content()) with a single click is an option, but I don't know how.

3) ???

有帮助吗?

解决方案

With or without clean URLs, you can pass variables based on your taxonomies through the links query string if you want to only return a single taxonomy term and style the page differently depending on the term.

$taxonomyTerm = $_GET['dm_issues'];

$args=array(
  'post_type' => 'dm_article',
  'dm_issues' => $taxonomyTerm,
  'post_status' => 'publish',
);

There is reference to this int he Wordpress 'query_posts' documentation by passing variable into your query parameters: http://codex.wordpress.org/Function_Reference/query_posts#Example_4

For instance in the link below, the title is generated based on the sting in the URL.
http://lph.biz/areas-we-serve/service-region/?region=Conestoga

You can set up a parameter that will return a default value if the page is reached without the variable being defined. See below:

if (empty($taxonomyTerm)) {
 $taxonomyTerm = 'Default Value';
} 

其他提示

You can create a separate page template. Define the template name at the top of your PHP document:

<?php
 /*
 Template Name: Printed Page Template
*/

Place your custom query, including all of the content that you need output in this page template... In your WP admin, create a new blank page and assign your new 'Printed Page Template' template to this page. Save it and view the page.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top