Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get page.php using php code

I am working with wordpress.

I see that in my index.php there is a code called <?php get_footer(); ?> ..and I get it, it's simple. This code will pull footer.php.

It seems to me that the get_footer() is built in to wordpress that pulls anything that is named footer.php.

I have created my own page called page.php.

I want to be able to 'get' this page and show in my php code enabled 'sidebar widget'.

I have tried to paste this code, and I am more that certain that its wrong:

<?php
echo file_get_contents("side.php");
?>

What code would I need if I want my custom page called page.php to be shown?

like image 969
Andrei Hambaleo Avatar asked Sep 18 '26 14:09

Andrei Hambaleo


2 Answers

The WordPress way to load a template file is get_template_part():

get_template_part( 'page' );
// loads page.php from the theme directory.

Note that page.php is a special filename in WordPress themes, this file is loaded as a template if a page is displayed. You should give your file a different name if you want to prevent this.

Details are in the already mentioned template-hierarchy.png

Edit:

After rereading your question: If your page.php is not from a template, but in a folder of a plugin you are developing as a sidebar widget, the also already mentioned include('page.php'); would be the way to go.

like image 147
Gerald Schneider Avatar answered Sep 20 '26 02:09

Gerald Schneider


page.php is a special page of wordpress. See this diagram. https://developer.wordpress.org/files/2014/10/template-hierarchy.png.

The wordpress way is to create a own template. https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/

like image 23
fito Avatar answered Sep 20 '26 04:09

fito