Loading page template into shortcode

I am attempting to load a page template into a shortcode so I can easily load the content wherever I want.

I have done some research and many people have said this code has worked for them but for some reason this does not seem to load my template right as I just get a blank page.

I know the shortcode is executing as it does not show as plain text so I’m guessing there is a problem with the way I am loading the template.

Any help is much appreciated .

public function register(){
        add_shortcode( 'sponsor_main_page', array($this,'my_form_shortcode') );
            $RegistrationFormId = esc_attr( get_option( 'ik_form_id' ) );
        }

function my_form_shortcode() {
        ob_start();
        get_template_part( 'template-sponsors.php' );
        return ob_get_clean();
    }

3 Answers
3

get_template_part takes slug as first parameter and not filename.

So it should be:

get_template_part( 'template-sponsors' );

And with more details… This function takes two parameters:

get_template_part( string $slug, string $name = null )

And inside of it, the name of a file is built like this:

if ( '' !== $name )

        $templates[] = “{$slug}-{$name}.php”;
 
    $templates[] = “{$slug}.php”;

So, as you can see, the .php part is added automatically. So your code will try to load file called template-sponsors.php.php and there is no such file, I guess.

Leave a Comment