You can get part of the way there by querying the page template using get_page_template_slug()
inside your loop. For example, you can do something for non-default page templates:
if ( '' != get_page_template_slug( $page->ID ) ) {
// This page uses a custom page template;
// do something
}
Or, you could look for your contact-form page template specifically:
if ( 'template-contact-form.php' == get_page_template_slug( $page->ID ) ) {
// This page uses the contact form custom page template;
// do something
}
The problem you’ll encounter is the do something part, because you’ve hard-coded your contact form into the template.
To do what you’re wanting to do, you’ll probably need to convert your contact-form code into a shortcode or Widget, so that you can parse/output it in your custom loop, such as:
if ( 'template-contact-form.php' == get_page_template_slug( $page->ID ) ) {
// This page uses the contact form custom page template;
// parse the shortcode
echo do_shortcode( '[contact-form]' );
}
…or:
if ( 'template-contact-form.php' == get_page_template_slug( $page->ID ) ) {
// This page uses the contact form custom page template;
// output the widget
the_widget( 'my-contact-form' );
}
(Note: shorthand code for do_shortcode()
and the_widget()
; refer to Codex for proper usage.)