Include another php file into the current php file

Description

The code <?php include( get_template_directory() . '/include-services-post.php' ); ?> is a PHP statement used in a WordPress theme file to include another PHP file into the current file. Here’s a breakdown of what each part of this code does:

  1. <?php ... ?>:
    • This is PHP opening and closing tags. Anything inside these tags is processed as PHP code.
  2. include(...):
    • The include function is used to include and evaluate a specified file in the current script. It essentially inserts the content of the specified file into the location where the include statement is used. If the file cannot be found or included, PHP will generate a warning, but the script will continue to run.
  3. get_template_directory():
    • This is a WordPress function that returns the absolute file path to the parent theme‘s directory.
    • For example, if your WordPress theme is located in wp-content/themes/my-theme/, get_template_directory() will return something like /home/user/public_html/wp-content/themes/my-theme/.
  4. '/include-services-post.php':
    • This is the relative path of the file to be included, starting from the theme directory.
    • So, in this case, the file being included is called include-services-post.php, and it is expected to be located in the root directory of the theme (same level as functions.php, for example).

How it works:

The code will:

  1. Use get_template_directory() to get the absolute path to the parent theme directory.
  2. Append the file path /include-services-post.php to it, resulting in a full file path.
    • For example: /home/user/public_html/wp-content/themes/my-theme/include-services-post.php
  3. The include() function will then include the include-services-post.php file at the location where this code is executed.

PHP Source Code

                        
                          <?php include( get_template_directory() . '/include-services-post.php' ); ?>                        
                        


Post Categories