Linking Theme Files & Directories
Linking to Core Theme Files
As you’ve learned, WordPress themes are built from a number of different template files. At the very least this will usually include a
sidebar.php, header.php and footer.php. These are called using Template Tags, for example:
You can create custom versions of these files can be called as well by naming the file
sidebar-{your_custom_template}.php, header-{your_custom_template}.php and footer-{your_custom_template}.php. You can then use Template Tags with the custom template name as the only parameter, like this:
1
2
3
| get_header( 'your_custom_template' );get_footer( 'your_custom_template' );get_sidebar( 'your_custom_template' ); |
WordPress creates pages by assembling various files. Aside from the standard files for the header, footer and sidebar, you can create custom template files and call them at any location in the page using get_template_part().
For example, if you would like to create a custom template to handle your post content you could create a template file called
content.php and then add a specific content layout for product content by extending the file name to content-product.php. You would then load this template file in your theme like this:
1
| get_template_part( 'content', 'product' ); |
If you want to add more organization to your templates, you can place them in their own directories within your theme directory. For example, suppose you add a couple more content templates for profiles and locations, and group them in their own directory called
content-templates.
To include your content templates, prepend the directory names to the
slug argument like this:
1
2
3
| get_template_part( 'content-templates/content', 'location' );get_template_part( 'content-templates/content', 'product' );get_template_part( 'content-templates/content', 'profile' ); |
Linking to Theme Directories #
To link to the theme’s directory, you can use the following function:
If you are not using a child theme, this function will return the full URI to your theme’s main folder. You can use this to reference sub-folders and files in your theme like this:
1
| echo get_theme_file_uri( 'images/logo.png' ); |
The Functions get_theme_file_uri(), get_theme_file_path(), get_parent_theme_file_uri(), get_parent_theme_file_path() were introduced in WordPress 4.7.
For previous WordPress versions, use get_template_directory_uri(), get_template_directory(), get_stylesheet_directory_uri(), get_stylesheet_directory().
Take note that the newer 4.7 functions run the older functions anyway as part of the checking process so it makes sense to use the newer functions when possible.
Dynamic Linking in Templates
Regardless of your permalink settings, you can link to a page or post dynamically by referring to its unique numerical ID (seen in several pages in the admin interface) with
1
| <a href="<?php echo get_permalink($ID); ?>">This is a link</a> |
This is a convenient way to create page menus as you can later change page slugs without breaking links, as IDs will stay the same. However, this might increase database queries.
Comments
Post a Comment