$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Loading page template into shortcode|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Loading page template into shortcode

matteradmin9PV0评论

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 . )

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far