Custom template file for displaying the inner blog contents - drupal

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.

Related

Changing the "standard template" name of the single.php post template

I have a Wordpress website, now i want to change the post template name how it displays in the Wordpress CMS. Now it's saying "Standard template", i want to rename this to "News", just for usability reasons.
I can't find a way to do this. I know you can create new post templates by creating new files, but it always takes the single.php as standard template. I'm also using a child-theme, so i dont want to delete the single.php file, just rename the text: "Standard template".
Thanks in advance.
I tried creating a new post template file with a custom title. This doesnt solve problem, as the single.php file will still be the standard one (i dont want the user to have to change the template).
You can use another single.php for another post type for example if you have news registered as a post type you could have a single-news.php and that file then would server all the single views of postype = news. But from what i understand you would like to create a template appearing to the user with a different name. For that the best practice is to create a directory inside your child theme and name it page-teplates. Inside this directory you can create as many different templates you want and wordpress will recognize them but adding the following code at the top of each template. For the sake of the example lets say i want to create a contact page template. I will create a contact.php file inside the directory page-templates and have these lines of code inside.
<?php
/**
* Template Name: Contact
**/
get_header();
/* My templates Code/Design Here */
get_footer();
The possibilities are endless.
The code in this post will generate a dropdown box similar to the one that you see in pages.
All it requires is a little editing of your child themes functions.php file .
In most cases, you just need to copy your single.php file to a new file name in your child theme and edit the functions.php file in your child theme. Name your templates as seen in the code Dimitrios posted.
consider using the premium elementor page builder to create custom post templates that you can apply at will.
Try a plugin like Post Custom Templates Lite

How do I display either body or file from content

I am a new in Drupal. I want your help for doing this please help
I have created one Content type with fields( Title, Body, File) and also added content through this content type. But Some of the content didn't have file filed and some of the content having the only file. In the view, I want to show both either body or file. if suppose file field is empty then it should display the body of the same content.
Thanks.
You could do this by creating a custom node template for particular content type. So if you have a content type 'news', for example, the you can create a node template for this content type and customized the front-end display of the content. See below how it would work.
As per the Drupal theme suggestions, create a node template, named, node--news.tpl.php (copying the existing node.tpl.php file) in your custom (active) theme directory under the templates folder. On this template, you will be able to access the $node object variable, containing complete node information including fields and data.
Suppose your fields are field_image and body then replace your template's code print render($content); with the following lines:
if(!empty($content['body']){
print render($content['body']);
} else {
print render($content['field_image']);
}
For more details: https://www.drupal.org/node/1323842
Hope this help!

Custom post type: creating a page template with the same slug

I'm currently working on a custom post type and want to be able to edit the archive page from Wordpress with a page template. So I created the CPT called 'cars' and created a page template with template name: 'Cars overview'. Next i create a page inside WordPress and choose the template page 'Cars overview' and gave it the URL: mywebsite.com/cars/
Now the problem is that the slug 'mywebsite.com/cars/' is already in use by the custom post type itself causing the page to load the custom post type loop instead of the page template loop. So I can't edit the title, content etc inside WordPress. I could change the url of the page, but i want to be able to control the overview page in WordPress.
Long story short: How can I create a page template that is using the same URL as the custom post type archive page?
Thanks in advance!
One simple solution, simply disable the archive where you create your custom post type:
register_post_type("cars", array("has_archive" => false));
Another approach rather then disabling the archiving and adding another page to show the cars. Changing the archive template used by your theme might be a better option.
First step is to find the template currently in use by your theme, copy it to your plugin file and you can change the template file to whatever you like. You can find more information about it here.
The only thing you need to do is point WordPress to the right direction:
add_filter("archive_template", "archive_template");
function archive_template($archive_template) {
global $post;
if ($post->post_type == "cars")
{
$archive_template = "path/to/your/template.php";
}
return $archive_template;
}
Disabling the archive and creating one manually seems a bit strange to me. And I always replace the archive page and sometimes single page from our theme (usually the7).

How to customise HTML structure in Drupal view block

I am currently developing a website in Drupal. I have a load of news articles which are of content type 'news'. I want to create a widget to show at the bottom of several pages listing the latest articles. I have managed to achieve thus far by creating the view with the fields I want and then making the view available as a block.
The problem is the format in which the articles are being listed in the view. Ideally I would like to customise the HTML. I understand that I can create a custom template to target views/blocks but I haven't quite got a full understanding of it yet, and find the Drupal documentation to be quite dry and therefore hard to find what I need from.
Any pointers would be helpful.
Go to your view edit page and select "table" as your view mode.
Go to "Theme information" and look for the desired (style output) template name.
Copy this name and create a new file in your theme folder using this name. I.e.: views-view-table--view-name-block.tpl.php.
In this file you can use the following structure (as an example) to get the values of the fields:
<?php foreach($rows as $row):?>
<div>
<h2><?php print $row['title'];?></h2>
<p><?php print $row['field_custom'];?></p>
</div>
<?php endforeach;?>
Don't forget press rescan theme files after you saved the file in the view theme section.
You can get as specific as you want. Create a block view next to the default or page view and you will see specific file names in the "Theme information" section.

Multiple thumbnails in WordPress based on page template?

Is there a way to add multiple thumbnails in a page based on page template or page ID
instead of post type (post, page)?
You can add a conditional statement to test for a specific page id:
if(is_page('<your page id>')){
// Do your thing
}
Similarly, you can test for a specific page template like so:
if(is_page_template('<page template file name>']){
// Go on wit' you bad self
}
Make sure that when you use is_page_template() that you feed it the file name of the template, e.g., is_page_template('two-columns.php'), as opposed to the name of the template, e.g., is_page_template('Two Columns').

Resources