简体   繁体   English

插件简码在页面编辑器(管理面板)Wordpress 中显示模板页面

[英]Plugin shortcode displays the template page in page editor (admin panel) Wordpress

I am creating a plugin for wordpress themes that loads the templates in its own directory rather than placing templates in the theme that makes me independent to include templates in themes, for that I have created shortcodes to load the different templates on conditions.我正在为 wordpress 主题创建一个插件,该插件将模板加载到自己的目录中,而不是将模板放在主题中,这使我能够独立地在主题中包含模板,为此我创建了短代码来根据条件加载不同的模板。 below is the code:下面是代码:

add_shortcode('template', 'add_template'); add_shortcode('模板', 'add_template');

function add_template( $atts) {函数 add_template( $atts) {
extract( shortcode_atts( array( 'template' => ''提取(shortcode_atts(数组('模板'=> ''
), $atts ) ); ), $atts ) );

switch ($template) {开关($模板){

 case 'template1': include 'templates/template1.php'; break; case 'template2': include 'templates/template2.php'; break; default: include 'templates/template1.php'; break; } }

My problem is in some themes my plugin start to display the page within the admin panel is there anything I am doing wrong?我的问题是在某些主题中我的插件开始在管理面板中显示页面是我做错了什么吗? please help....请帮忙....

Found a solution we just need to add a check that the user is not admin before including template.找到了一个解决方案,我们只需要在包含模板之前添加一个用户不是管理员的检查。

add_shortcode('template', 'add_template');

function add_template( $atts) {
extract( shortcode_atts( array( 'template' => ''
), $atts ) );

switch ($template) {

  case 'template1':
     if ( !is_admin() ) {
         include 'templates/template1.php';
     }
     break;

  case 'template2':
       if ( !is_admin() ) {
        include 'templates/template2.php';
       }            
       break;

  default:
     if ( !is_admin() ) {
       include 'templates/template1.php';
     }        
     break;   
   }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM