It is possible to choose different templates when I am creating a new node?
For example: When i want to create a node of Content Type Product, i want to choose between 4 different templates.
I have two answers:
1) create a CCK field that the user will choose what template to use, (e.g. field_template), and add this snippet in your template.php:
function mytheme_preprocess_node(&$vars) {
if (!empty($vars['field_template'][0]['value'])) {
array_unshift($vars['template_files'], 'node-' . $vars['field_template'][0]['value']);
}
}
That will then try to use node-template.tpl.php as your template file, and will fallback to node.tpl.php if it doesn't find it.
2) create a Taxonomy for this content type, and design by the taxonomy (something like the code above, only a little modified).
Related
Is it possible to assign different templates to child pages of post types?
The main purpose is to keep a clean URL but also differentiate the child pages from the parents (with the use of ACF).
Of course this is possible, there are a lot of different possibilities how to achieve this.
First of all lets have a look on the WordPress template hierarchy which you really should understand:
As you can see there is a single-$posttype.php template (replace $posttype with your post type slug). This is what you should use to differentiate templates between post types.
If you want to differentiate templates within the same posttype I would recommend the following implementation within single-$posttype.php:
if(get_post_meta($post->ID,'template',true)=="templateA") {
get_template_part("templateA");
} else if(get_post_meta($post->ID,'template',true)=="templateB") {
get_template_part("templateB");
} else {
get_template_part("templateDefault");
}
Adjust this code to your needs.
I am adding a page with a node-id and earlier I was using "page--node--node-id.tpl.php" template to customize the node/page. However, I accidentally deleted that node, and now I am unable to create that node with particular nid node. I want to know how to customize the specific page as each page his unique title.
Use page--node.tpl.php for all the nodes instead of each template otherwise use
node--[node-type].tpl.php for specifc node content type template
There is another smart step to create content type template with your own specific name by following below code
function themeName_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
$vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);
}
}
In above case If node content type is "article" then the template suggestion will be "page--article.tpl.php".
Create template file page--nodetype.tpl.php in your theme directory.
I have a custom Taxonomy. The lists of terms in the taxonomy will be dynamically created.
I have a custom template for Taxonomy as taxonomy-{custom-template}.php, but I do not want to use the same template for the terms page.
I will not be able to create template for terms upfront as taxonomy-{custom-template}-{term}.php as the terms are created dynamically.
Is there any option to exclude or have a generic template for the terms?
I'd use is_tax() in your taxonomy-{custom-template}.php to determine whether you are displaying the archive page or the terms page, and them include the relevant code in template parts.
Create 2 new template parts in your template parts folder - lets say its called partials
One is for your taxonomy archive page e.g. tax_archive_content.php.
Copy the archive-specific code from taxonomy-{custom-template}.php
into it.
The other is for your terms page - e.g. tax_terms_content.php. Put your new code for handling the terms in here.
Add the following to your taxonomy-{custom-template}.php
(This will replace the archive-specific code that you moved to tax_archive_content.php)
if (is_tax())
get_template_part('partial/tax_archive_content');
else
get_template_part('partial/tax_terms_content');
References for is_tax:
Codex: https://codex.wordpress.org/Function_Reference/is_tax
Developer.wordpress.com:
https://developer.wordpress.org/reference/functions/is_tax/
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.
I have enabled taxonomies in the form of Tag, and I would like the tag field to show up below the content when users are editing the page. Where would I make that setting change?
For editing? You don't need code at all, provided you use the CCK module on your site.
Go to Admin > Content Types. Click on "manage fields" on the content type you want to edit, then drag the Taxonomy module form underneath the body. Hit Save, you're done.
For maximum control, you can also use the Content Taxonomy module which turns taxonomies into CCK fields.
You'll need to implement hook_form_alter() within a custom module to adjust the weights of the node edit form fields:
/**
* Implementation of hook_form_alter().
*/
function yourModule_form_alter(&$form, $form_state, $form_id) {
// Is this a node edit form?
if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
// Yes, adjust taxonomy weight to push it to bottom of form
$form['taxonomy']['#weight'] = 42; // TODO: Find appropriate number by inspecting the other form element weights
}
}
Edit: As bmann pointed out in a separate answer, this is not necessary if you have installed the CCK module on your site, as it adds a configuration option for the field order under 'admin > content types > manage fields'.
This is a sample from one of the node.tpl.php files in the themes/ directory.
<div class="taxonomy"><?php print $terms?></div>
If you move that code below
<div class="content"><?php print $content?></div>
It should work.
Assuming I've understood your question correctly!