Php links in wordpress

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.

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.

@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!

3 Answers 3

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

Читайте также:  Html скрипт формы ввода

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.

Add Page Template

Thank you. this is perfect! I still need to create the page from wp-admin in order to get a custom template to work! This worked perfectly!

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

mention your template name here

You can also review wordpress documentation for same.

Thank you, I did try this and I have tried it again just now but it is not working. The link keeps going directing me to index.php

I have provided same solution to you but it did not work for you .when some else provided , it worked for you.. great.

@shishirmishra I feel for you, happened to me too 🙂 But I guess the accepted answer explained better + the screen shot helped perhaps. Anyways, +1 from me for future motivation 🙂

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.

php file to be included for example is some-file.php inside root folder. then you would write

get_template_part( 'some', 'file' ); 

Источник

What is the best way of linking to WordPress pages with PHP? Considering that I move the page from a local server to a live server to another URL?

I know it might seem like a silly question, But I would like to know what is the best solution for linking to pages, Do you link directly to a ID or a page name. I don’t understand the WordPress Codex page. Could someone please give a PHP example of this.

4 Answers 4

If you know the Page $id , use get_permalink() :

If you know the Page $slug , such as /about (including hierarchy, such as /about/work ), use get_page_by_path() to determine the Page $id , then use get_permalink() .

ID; $permalink = get_permalink( $page_id ); ?> 

If you know the Page $title , such as «Some Random Page Name», use get_page_by_title() , then use get_permalink() :

ID; $permalink = get_permalink( $page_id ); ?> 

Do you want to find them by name? If yes then you can

If you want to hardcode ‘/page-names’ you can use home_url(‘/wordpress/services’) function with esc_url() for sanitizing URLs — it will always get you full url of home page ( local or live )

You can use a shortcode to insert the domain name into the internal link and then just add the page url on the end e.g [domain_name]/page-name.

Add this code into your child themes function.php and it’s ready to go!

//add shortcode that displays current site name function domain_name() < $currentDomain = "yoursite.com"; return $currentDomain; >add_shortcode('domain_name', 'domain_name'); 

I’ve tested this on multiple live sites and it appears to be working perfectly.

Hope this is what you’re looking for!

Источник

Выводит ссылки навигации по страницам, для многостраничных постов (для разделения используется

Оцените статью