dousao2186 2019-02-18 12:06
浏览 189

将页面模板加载到短代码中

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();
    }
  • 写回答

1条回答 默认 最新

  • doulu0266 2019-02-18 14:08
    关注

    Try to explain more precisely what you're trying to do. Where did you put this code? What does your template contain and what is its purpose?

    First with get_template_part you have to remove .php

    And i don't know what type of template you want to load. Your code seems want to pass a variable to the template. And this code seems to be a methods of a Class.

    Maybe you just put this code on functions.php, that's why you get a blank page.

    Try with this code on functions.php

    add_shortcode('my-shortcode', 'display_my_template');
    function display_my_template() {
        ob_start();
        get_template_part( 'template-sponsors' );
        return ob_get_clean();
    }
    

    And then you can use the shortcode in your page like that [my-shortcode]

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值