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.

Read More

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?

Related posts

7 comments

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

  2. try the following

    include ('side.php');
    

    OR

    require ('side.php');
    

    you may also use include_once / require_once

  3. Please use require_once or include in php file like below.

    require_once('side.php');
    

    or

    include ('side.php');
    
  4. try this,

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

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

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

Comments are closed.