3

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?

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78

7 Answers7

2

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.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
1

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/

fito
  • 116
  • 9
0

try the following

include ('side.php');

OR

require ('side.php');

you may also use include_once / require_once

prats1411
  • 162
  • 12
0

Add this code in your php page

<?php include_once('filename.php'); ?>
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
0

Please use require_once or include in php file like below.

require_once('side.php');

or

include ('side.php');
Jalpa
  • 697
  • 3
  • 13
0

try this,


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

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

0

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.

Community
  • 1
  • 1
devpro
  • 16,184
  • 3
  • 27
  • 38