I know it is possible to have enable post thumbnails on a specific post type, but is there any way of doing this just for specific template?
2 Answers
You can use the remove_post_type_support
for your requirement. In your case, you remove post thumbnail if specific page template is not selected.
function remove_post_thumb(){
if (isset($_GET['post'])) {
$post_id = $_GET['post'];
} else if (isset($_POST['post_ID'])) {
$post_id = $_POST['post_ID'];
} else {
return;
}
$template_file = get_post_meta($post_id, '_wp_page_template', TRUE);
if ($template_file != 'page-your-template.php') {
remove_post_type_support('page', 'thumbnail');
}
}
-
Thanks for this - just tried it, but it doesn't seem to be working (thumbnail still shows regardless of template). Jun 28, 2015 at 11:14
-
if your template is in some folder then your condition should be
folder/page-your-template.php
and also the post meta_wp_page_template
might not work unless you save the post once. Try once.– KarunJun 28, 2015 at 11:20 -
You can use a conditional function to check whether the page being accessed is using a page template and if so, display .... code.
is_page_template( 'about.php' )
The above is the specific function and it could be used in an if statement, like so:
if ( is_page_template( 'my_template.php' ) )
{
// Display Code
}
You could also do an if/else and display something as an alternative:
if ( is_page_template( 'my_template.php' ) )
{
// Display Code
}
else
{
// Display Alternative Code
}
Just the same, instead of checking whether the current page is using the page template, you could check to see if it's not:
if ( !is_page_template( 'my_template.php' ) )
{
// Display Code or Error
}
More information on Conditionals: https://codex.wordpress.org/Conditional_Tags#Is_a_Page_Template