Show form based on (custom) post type

I created two custom post types:

  • Online training
  • Classroom training

I created two different forms, one for each post type. On one (enrollment)page I would like to

  • Show form 7 if post type = online training (and hide form 1)
  • Show form 1 if post type = classroom training (and hide form 7)

Tried to create a shortcode with an if statement (see code below). This “shortcode in shortcode” approach changes the whole layout. Not what I like.

function custom_select_gf_form () {
	ob_start();
	$post_id = get_query_var( 'training' );
	if ( get_post_type( $post_id ) === 'onlinetraining' ) { 
		echo do_shortcode ('[gravityform id="7" title="false" description="false" ajax="true"]');
	}
	else {
		echo do_shortcode ('[gravityform id="1" title="false" description="false" ajax="true"]');
	}
	$content = ob_get_clean(); // store buffered output content.
	return $content;
}
add_shortcode( 'enrollment', 'custom_select_gf_form' );

Does someone know an alternative approach?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.