I am trying to use get_template_part when creating a page in the admin section of my site. However, it does not seem to be working. Does this function not work in the admin section? What alternative can I use?
1 Answer
Yes, get_template_part() does work on Admin pages. Here is how I tested:
Add this to functions.php theme (or child theme) file:
add_action( 'admin_menu', 'wpse_99662_register_admin_test_page' );
function wpse_99662_register_admin_test_page() {
add_menu_page(
'Admin Test Page',
'Admin Test Page',
'manage_options',
'admin_test_page',
'wpse_99662_admin_test_page'
);
}
function wpse_99662_admin_test_page() {
echo '<h2>Admin Test Page</h2>';
get_template_part( 'admin', 'test' );
}
The admin-test.php
file contains the following:
<?php
echo "Loaded admin-test.php<br />";
-
I was using it like this - <?php get_template_part( THEME_DIRECTORY . 'includes/test-components/test-badge' ); ?> - so I just passed in a single parameter with the full path - is that a problematic syntax?– WilliamMay 16, 2013 at 13:19
-
get_template_part) hands its result to locate_template() which is already checking inside STYLESHEETPATH and TEMPLATEPATH for each file name. So, now you are checking for another THEME_DIRECTORY inside each of those directories which probably doesn't exist. For that case,
locate_template()
silently fails. Try:get_template_part( 'includes/test-components/test-badge' );
May 16, 2013 at 14:10