How to link to a custom .php page in my folder

admin2025-06-05  1

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.

Share Improve this question asked Feb 21, 2017 at 16:38 Martina SartorMartina Sartor 331 gold badge1 silver badge4 bronze badges 3
  • What is the purpose of that page? I mean why just don't create a page within WordPress? – Laxmana Commented Feb 21, 2017 at 16:45
  • @Laxmana Because i have some custom code in there to create a PDF from html. I am passing an ID via the url, then I use the ID to find the page i want to convert and I use some code to convert the html into PDF. Please let me know if i havent been too clear! – Martina Sartor Commented Feb 21, 2017 at 16:48
  • 1 You can do it through WordPress custom template pages or WordPress endpoints – Laxmana Commented Feb 21, 2017 at 16:52
Add a comment  | 

3 Answers 3

Reset to default 6

WordPress doesn't load templates that way.

First: give the template a name:

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.

Then, assign the template to a page:

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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749117386a316510.html

最新回复(0)