Andrei Hambaleo
Andrei Hambaleo

Reputation: 63

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?

Upvotes: 3

Views: 113

Answers (7)

devpro
devpro

Reputation: 16117

There are so many options that you can chose:

You can use get_template_part():

From the Docs: WordPress now offers a function, get_template_part(), that is part of the native API and is used specifically for reusing sections - or templates - of code (except for the header, footer, and sidebar) through your theme.

Other solution is require_once() or include_once(). But if i compare both function include_once() is faster than to require_once() for smaller application.

For better understanding about the require and include you can read this Question.

Upvotes: 0

Gerald Schneider
Gerald Schneider

Reputation: 17797

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.

Upvotes: 2

Amarinder Singh Thind
Amarinder Singh Thind

Reputation: 140

Add this code in your php page

<?php include_once('filename.php'); ?>

Upvotes: 0

siversansar
siversansar

Reputation: 11

try this,


  require( dirname( __FILE__ ) . '/page.php' );

if your page is in same level where is your parent page.

Upvotes: 0

Jalpa
Jalpa

Reputation: 720

Please use require_once or include in php file like below.

require_once('side.php');

or

include ('side.php');

Upvotes: 0

fito
fito

Reputation: 116

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/

Upvotes: 1

prats1411
prats1411

Reputation: 162

try the following

include ('side.php');

OR

require ('side.php');

you may also use include_once / require_once

Upvotes: 0

Related Questions