Can child single post types have a different single-post_type template (Custom Post Types)? - wordpress

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.

Related

Override item-list.html.twig & time.html.twig inside a view

I've created a view that outputs a <ul> with a <time> element in each <li>. I need to convert the HTML list to a select.
So I did 2 things:
Copied item-list.html.twig and changed <ul> to a <select>
Copied time.html.twig and changed <time> to <option>
Although this works I need those 2 templates to be scoped to my view named: 'vcon-selection-table'. I tried many things but nothing works:
views-view-table--vcon-selection-table--item-list.html
views-view-table--vcon-selection-table-item-list.html
views-view-table--vcon-selection-table.html--dataset-item-list
So the question is: What template name should I use to override the templates item-list.html.twig and time.html.twig inside my view?
Is the purpose of that view to only provide that <select> element and <option> elements? Then this really isn't the way to go. Don't build a form or form elements with Views.
You'd better provide a custom form or a custom item_list in a custom block. The block then can be placed where ever you need it. There are plenty of tutorials out there. Maybe take the following as a starting point: https://valuebound.com/resources/blog/creating-a-custom-form-in-a-block-in-two-steps-in-Drupal-8
Otherwise, if you really want to continue your road I think you have to simply preprocess the list and time fields to switch to a different template according your desired conditions. I strongly guess there is no option that a custom template for a field from a node in a view will automatically be taken into account by naming patterns. You have to add that template suggestion manually first.
Snippet to be placed in MYMODULE.module file or MYTHEME.theme file.
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function MYMODULE/MYTHEME_theme_suggestions_item_list_alter(array &$suggestions, array $variables) {
// Use \Drupal::routeMatch() to get the current view ID or some other indicator.
// Sample: Get the current view ID.
$view_id = \Drupal::routeMatch()->getRouteObject()->getDefault('view_id');
// Install Devel, Kint module, then uncomment the following line and flush caches to inspect the outcome.
// ksm($view_id);
// Add the template suggestion.
// Add your template now under /templates/item-list--VIEWID.html.twig
if (!empty($view_id)) {
$suggestions[] = 'item_list__' . $view_id;
}
}
Downside of this approach is that this would trigger all item_lists that live on that exact page/view to take this template. You could fine-grain your templating much better with a custom module and a custom item_list and/or form.
Alternatively you can also move the logic to the date field and a custom view mode and an extra template for that. Then at least it stays where it belongs to.
To enable Twig debugging and getting the built-in template suggestions printed as HTML comments follow this article: https://www.drupal.org/docs/8/theming/twig/debugging-twig-templates

Custom Post Type - Single-{slug}.php to override Single-{$posttype}.php in WordPress

I am looking to add a custom template for a single post inside of a Custom Post Type. The Custom Post Type is working as it should and all of the posts are correctly using single-{$posttype}.php. However, for the page with the slug "our-wedding", I am trying to override single-{$posttype}.php and have it use single-our-wedding.php. However, the page is still using single-{$posttype}.php.
Any ideas?
You can either use a solution that will let you assign custom templates to one particular post of that post type (there are plugins).
Or you can edit the single-{$posttype}.php to include a
if( is_single('our-wedding') ){
get_template_part( 'my-template');
} else {
// The usual code for this single-posttype
}
And then create a file called "my-template.php" inside your theme folder.
(Edited based on user feedback.)

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.

Drupal: Content Type templates

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).

Display a text snippet if one of the taxonomy terms is 'green'?

I want to be able to show or hide a text snippet on a node based on whether or not it was tagged with a specific taxonomy term.
Something along the lines of:
if (term('green')) {
echo"this is green";
}
else {
echo "not green";
}
What is the Drupal way of doing this?
Assuming from your example that you're working from inside of node.tpl.php, and that you have a single very specific term in mind:
You can access $taxonomy->taxonomy_term_YourTermID from within your node.tpl.php file, and test based on that.
If you'd like a more abstract solution, I'd recommend installing the devel module and building a function in your template.php files based on what shows up under the 'Devel' tab in the relevant node.

Resources