php - Loading page template into shortcode

admin2025-06-03  3

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();
    }

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();
    }
Share Improve this question edited Feb 18, 2019 at 17:14 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 18, 2019 at 17:04 Ibrarr KhanIbrarr Khan 332 silver badges7 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

It's likely to be occurring because you are adding .php to the parameter in get_template_part(). This is how I would write the code for your question:

Prerequisites for this example to work:

  • Create a folder in your theme directory called template-parts.
  • Create a new file in this directory called template-sponsors.php
  • This is assuming your shortcode is [sponsor_main_page]

Code to put in your functions.php:

function custom_theme_load_sponsors_template() {
  ob_start();
  get_template_part( 'template-parts/template-sponsors' ); 
  return ob_get_clean();
}
add_shortcode( 'sponsor_main_page', 'custom_theme_load_sponsors_template' );

I like using locate_template, rather than get_template_part . The locate_template will do the equivalent of a PHP include() or include_once(), which is what I think you are aiming for.

locate_template("mycustomfunctions.php",true,true);

Put your mycustomfunctions.php file in the active template folder (optimally, a Child Theme). Then put the locate_template() command in your template, at the very top (after the comment block). Any functions in your mysustomfunctions.php file will then be available to your template.

The locate_template function is called by get_template_part. But the parameters allow you to define how the include works. See https://developer.wordpress/reference/functions/locate_template/ for more info.

(Added See the accepted answer to this question: Get template part vs locate template function . )

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748888716a314560.html

最新回复(0)