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:
<?php ... ?>:- This is PHP opening and closing tags. Anything inside these tags is processed as PHP code.
include(...):- The
includefunction 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 theincludestatement is used. If the file cannot be found or included, PHP will generate a warning, but the script will continue to run.
- The
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/.
'/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 asfunctions.php, for example).
How it works:
The code will:
- Use
get_template_directory()to get the absolute path to the parent theme directory. - Append the file path
/include-services-post.phpto it, resulting in a full file path.- For example:
/home/user/public_html/wp-content/themes/my-theme/include-services-post.php
- For example:
- The
include()function will then include theinclude-services-post.phpfile at the location where this code is executed.
PHP Source Code
<?php include( get_template_directory() . '/include-services-post.php' ); ?>