I have created a custom page that I have called mypage.php.
It is in my template folder with al the other pages (index.php, page.php, ...)
I want this page to be opened when i click on the below code.
<a href="<?php site_url(); ?>/mypage.php">Go to page</a>
When I click on the link the url in my browser look like: http://localhost:8888/mypage.php which I guess is correct.
BUT it uses index.php as template ignoring the code I have into mypage.php
So all i am getting at the moment is an empty page with only my header and footer.
Is it possible to use pages in this way with Wordpress? I had a look online and on this site but I haven't been able to find a solution for this problem.
I have created a custom page that I have called mypage.php.
It is in my template folder with al the other pages (index.php, page.php, ...)
I want this page to be opened when i click on the below code.
<a href="<?php site_url(); ?>/mypage.php">Go to page</a>
When I click on the link the url in my browser look like: http://localhost:8888/mypage.php which I guess is correct.
BUT it uses index.php as template ignoring the code I have into mypage.php
So all i am getting at the moment is an empty page with only my header and footer.
Is it possible to use pages in this way with Wordpress? I had a look online and on this site but I haven't been able to find a solution for this problem.
WordPress doesn't load templates that way.
To load a page template, first you'll have to make sure your template has a name. To do that, you'll have to have the following CODE in your template file (here mypage.php
):
<?php
/**
* Template Name: My Page
*/
Once you have the above PHP comment, WordPress will recognise it as a template file.
Now you'll have to create a WordPress page
(from wp-admin
).
While you create the page
, WordPress will give you the option to choose a custom template (if there is one). After you choose the template and Publish
, that page will use your mypage.php
file as a template.
@Martina Sartor, you can use wordpress custom template process. Add a the below comment to the file you wish to make your custom page. Then go to the admin area and create a page, and select your custom template for that page.
Please review
<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, event
*/
// Page code here...
mention your template name here
You can also review wordpress documentation for same.
https://developer.wordpress/themes/template-files-section/page-template-files/
what you are looking to do is inserting custom php files inside your theme but sadly WP will not let you use .php files easily but fortunately it provides an alternate and more efficient way for including .php files.
WordPress provides get_template_part
function & custom page templates
. you can use these methods instead.
above answer has explained page templates and in addition to that you can use your custom php files included into that template or anywhere else using get_template_part
function. this works same as require
or include
native php functions.
template
<?php
/**
* Template Name: My Page template
*/
php file to be included for example is some-file.php
inside root folder. then you would write
get_template_part( 'some', 'file' );