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

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.

Related

Custom template file for displaying the inner blog contents

I am trying to create a custom layout for my blog listing and blog content display pages.
The following url will display all the latest blogs based on the conditions that I have set.
http://fqdn/general-blog
When I click on the 'Read more' button corresponding to a button, it will display the full blog contents in another page.
Sample format : http://fqdn/general-blog/tourist-details
I am able to modify the layout of the page corresponding to blog listing. For this, I have created a custom tpl file : page--general-blog.tpl.php.
What I want is to create a new template file for displaying the contents of individual blogs.
The individual blog content display page is not entering - page--general-blog.tpl.php file. It's always using the template - page.tpl.php.
I tried creating separate tpl files like :
page--general-blog--%.tpl.php
But, the changes in this template is not getting effect in the individual blog content display page.
How should I move forward. Has someone had any similar situations?
If your content type has name general-blog,you should name single blog entry template as node--general-blog.tpl.php. See
Theming nodes by content type for details.
If you need to theme whole page template depending on node type, you should add following code in your template.php:
function YOURTHEME_preprocess_page(&$variables) {
if (!empty($variables['node']) && !empty($variables['node']->type)) {
$variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
}
}
And then use page--node--general-blog.tpl.php file (see Page templates depending on node type for details).
Don't forget to clear the theme registry after creating new template file.

Specialized template for subpages?

If I have a page named 'events' (of which the specialized template will be named 'page-events.php'), is there any way to have a specialized template of a subpage 'events/event-sub-page'?
I can't seem to find how to do it in the documentation here: codex.wordpress.org/Page_Templates.
You would just create a new template called events-subpage.php and build it accordingly. Then when making your page in wordpress, select the template events-subpage.
You can just create the "event-sub-page" page in WordPress, set its parent to the "events" page, and make your custom template file "page-event-sub-page.php" (no need to specify the "events" part in the actual php filename)
Or in my case I wanted "../login" for a specialized login form and "../login/recovery" for password recovery. The template page is simply "page-recovery.php"

add custom field template to theme admin page

in WP admin how to add the custom field template plugin to a theme page?
it automatically shows in posts and pages but i want this in the theme page. the theme am using is from iwak "creations" portfolio page.
what files do i need to modify to add this?
It's very hard to say what you need to modify without being able to look at the code. Being a premium theme, we can't just download it and take a look.
Having said that, the theme may use the WordPress custom post type functionality. Search the code for a call to the register_post_type function. If it's used, you may be in luck. Either
add 'custom-fields' to the supports argument in that call, or
call add_post_type_support after the post type is registered. The $post_type parameter will be the first value passed to the register_post_type function, and the $supports parameter will be 'custom-fields'.
Daniel, are you using this Custom Post Type Plugin - http://wordpress.org/extend/plugins/custom-field-template/screenshots/? I've used it before, and the guy who created it is Japanese, so his personal page isn't very useful as far as support for english goes.
I had some trouble with this at first. But what I think you're trying to do is add the custom fields to your new pages you've created, correct?
It's not very straightforward, but once it works it's pretty awesome.
Basically, when you set up the plugin you create these different "Custom fields," right? Well part of that should look like this:
[Custom Field Name]
type = textarea
rows = 4
cols = 40
tinyMCE = true
htmlEditor = true
Ok, so once you've created those "Custom fields" keep note of the part in brackets. You'll add this to your template pages:
<?php getCustomField('Custom Field Name'); ?>
Now when you enter the info in the pages or posts, the content should appear as you've entered it.

Creating a link field in a custom content type

I have several custom content types and in one specific one I need to offer two fields, one for the href for a link, and one for the text of a link this way I can build and style it with minimal user involvement of the HTML/CSS. I also have a custom node.tpl for this content type. My problem is that drupal throws divs around each field I create that aren't in my template file for this content type (node-custom.tpl) and I can't put the href for a link with divs around it inside google.co.uk</div>"> See my problem. Maybe I'm doing this all wrong so any other ideas are welcome.
Please note I'm trying to create this site with minimum involvement of HTML/CSS access for the user. I'm aware I could hand code the link in the field.
The easiest way to do this would be to use a preprocess function in your template.php file and build the link up manually:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type = 'my_type') {
$uri = $node->field_name_of_link_field[LANGUAGE_NONE][0]['value'];
$text = $node->field_name_of_display_text_field[LANGUAGE_NONE][0]['value'];
$vars['my_link'] = l($text, $uri); // Using Drupal's l() function to render a link
}
}
Then in your node template file you'll have access to the variable $my_link which you can output anywhere, and will contain the correct HTML for the link. Finally, go to the "Manage Display" page for your content type and set the display of the two fields you no longer need to output to 'Hidden'.
There are other ways so if that's no good let me know
EDIT
Just to add, I think the easiest way to do this would actually be to install the Link module and use the provided field type instead of the two other fields you're currently using.

How to get $_REQUEST[] through URL in wordpress

Hi I have a problem in my site on which am working on localhost. I am using pretty urls in my localhost.I have custom page templates for all the sites every page has a different header and footer and the content in it.
I used
<div class="btn_container"></div>
<div class="btn_container"></div>
The Problem I have to get request parameters in wordpress and query the db against these request query string parameters. And also it does not route to this page template.
I have hard-coded URL like in the above divs.
Please help
Could you simply grab the link id from the URL like
$link_id = $_GET['page_id']; // this would equal 1038 from your example link
A page template is assigned to the page through the admin panel:
1. Go to the admin panel
2. click on pages
3. choose the page you have created
4. choose the template on the right hand side.
Simply linking to it does not assign the template to the page like your example is showing.
Try
$pageId = get_query_var( 'page_id', 0 );
get_query_var() only retrieves public query variables that are recognized by WP_Query.
This will not work with your custom variables.
If you want to get your custom variable check wordpress documentation - https://codex.wordpress.org/Function_Reference/get_query_var

Resources