Where should WordPress custom page template be placed? - php

I am just asking about custom page template:
If I put it in the subfolder it will work or it should be placed in the root directory.
For example, I created a template named custom-page-awesome.php and placed it in nytemplate directory.
Please let me know that it's automatically detected when I edit some page or need to include using custom code.

WordPress recognizes the subfolder '/page-templates/'. Therefore, it’s a good idea to store your global page templates in this folder to help keep them organized.: https://developer.wordpress.org/themes/template-files-section/page-template-files/

I believe WordPress does not recognize template files that are not in the root directory of the theme. That is unless you use the following function get_template_part( 'your-template-file' );
more about that here: https://developer.wordpress.org/reference/functions/get_template_part/

You need to put this commented line on top of your template php file in the root of the theme folder. It will display the template in the dropdown menu of pages in wordpress backoffice :

Related

How do I organise my page.[slugURL].php files into sub directories in WordPress?

In the interest of keeping my WordPress theme files organised, I am looking to create the following structure on my server:
/httpdocs/wp-content/themes/ and then the following folders within ...
theme name folder
bootstrap
content
css
js
templates
pages
I have managed to get my folders working correctly, with the exception of the 'pages' folder. I want my 'pages' folder to house all the 'pages-[slugURL].php' files. When placed in the '/httpdocs/wp-content/themes/theme-name' directory, the files are recognised and displayed accordingly in the browser. Unfortunately, when placed in the 'pages' folder, they are not recognised.
Is there a work around for this? I realise that I could simply leave the affected files where they are, but as this is my first attempt at making a WordPress theme from scratch, I would like everything as organised as possible, as to reduce any confusions in the future. Furthermore, I believe this would be just good practice anyway.
It would be appreciated, if anyone could let me know if there is a workaround on this.
Regards
Craig
There is a way around that. You can create a file called page in default directory with this content:
<?php
global $post;
$post_slug=$post->post_name;
get_template_part( 'pages/page', $post->post_name );
?>
And then just place all your page-[slugURL].php files into the pages directory.
But I am not suggesting to do this, because it is not following standard WordPress Template Hierarchy.
The WordPress template hierarchy does not allow you to this easily. You can read all details on the template hierarchy in WordPress here.
If you want a special folder for your pages, you need to use template_redirect action to help WordPress to find the template you choose for those pages. All details here.
Hope it helps!

Wordpress: Accessing templates stored inside a subfolder in the themes folder

I'm currently using the twentyfifteen theme to create my application. Now, inside the twentyfifteen themes folder, I have another folder called tuto, inside which is a template that I want to use, named oauth.php. Here's a couple of screenshots to give an overview of my project's file structure:
Now, in order to access my oauth.php file, I tried accessing the following link:
http://localhost/wordpress/tuto/oauth.php
However, I get a Object not found! page instead.
I have just started working with Wordpress, and would appreciate some help as to how to access templates stored inside a sub folder which is stored inside the main themes folder.
Login into admin and create new page. on right side in select template section you can see your created templates. select your created template and create new page. then you can access your create template just visiting this page.
just read it
https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/

Codeigniter view in Wordpress

I have a website build with Codeigniter and I need to have a blog. It should be a WordPress so what I decided to do is to add "blog" in my .htaccess and install a WordPress in myweb.com/blog
It works perfect.
Now, I need to change my theme header and footer from the one that I have already in my web.
How can I do it?
I have the view at views/common/header.php, but obviously, I can not load it like this in template/header.php
<?php $this->load->view('common/header');?>
If I'm not wrong, I should add any code at header.php in my template folder. So, the question is, what should I change or add on it??
Thanks a lot!
UPDATE
I think there should be a helper I could load in codeigniter in order to call that functions on Wordpress
How can I do it?
You directly cannot call CI view in wordpress file. Inorder to make changes to wordpress header and footer you will need to modify header.php and footer.php file under wordpress theme (wp-content/themes/your_theme_folder).
Also make sure that you do not modify wordpress default theme directly as any changes to default theme would be lost during wordpress updates.
Best practice is to create child theme and then make your changes over there
https://codex.wordpress.org/Child_Themes , here the guide to create child theme.

Wordpress - all pages link to the index.php file

[NEW AT WORDPRESS]
I'm creating my own wordpress theme with own css etc etc. I've managed to get everything good in the index.php file, and I'm trying to make the other pages as well now.
When I make the home.php, blog.php, about.php and contact.php file (in my theme folder) they don't link to it. I'm following the "WordPress 3: Creating and Editing Custom Themes with Chris Coyier" on Lynda.com and the example shows that whenever you make a file with the same name as your page, it takes that directly (which works at his tutorial).
Anyone that could know what is going on?
Example:
I got a file blog.php in my theme folder, and when I go to www.mydomain.com/blog the loaded file is the index.php file, instead of the blog.php file
There are two ways you can get this working using custom page templates.
Create a template for one specific page using the page slug or ID. In this case, change the name of the php files like this to match the name of the page you created in the UI: page-home.php, page-blog.php, page-about.php and page-contact.php
Much more flexible is to create a custom template that can be used on ANY page. Just add the template name to the top of the php file like so (inside the php block):
/*
Template Name: My Home Page Template
*/
Then edit the pages and select your custom template from the template dropdown menu (on the right hand side I think, if its visible).
Reference this page for more info: https://developer.wordpress.org/themes/template-files-section/page-template-files/
Found the solution to my (silly) problem.
Creating a page in your theme directory & adding the template comment at the top of your php file isn't enough. You need to go to the admin panel->pages->YOUR PAGE-> and check out the page attributes. There you can link the page to a certain template: http://d.pr/i/a0m0

Need to include a custom template page through a plugin

is there any way to include a custom template from a folder inside my plugin page into the Wordpress system so that when I, for example create a new page/post, it can show as one of the template choices other than default of course.
I have seen this link here WP - Use file in plugin directory as custom Page Template? but does not seem to talk about the stage were Wordpress checks for custom templates in the themes directory and how to make it look else where.
It's not in the documentation because WordPress doesn't look elsewhere. Your best bet would be to create a template page in your theme folder, call it what you want, and then use include() to pull the contents of your template file in the plugins directory into the file in your theme directory, which would allow WordPress to see it.
I think the only other way to accomplish this would be to mess with Core, which is very bad practice.

Categories