繁体   English   中英

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

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

我正在为 wordpress 主题创建一个插件,该插件将模板加载到自己的目录中,而不是将模板放在主题中,这使我能够独立地在主题中包含模板,为此我创建了短代码来根据条件加载不同的模板。 下面是代码:

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

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

开关($模板){

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

我的问题是在某些主题中我的插件开始在管理面板中显示页面是我做错了什么吗? 请帮忙....

找到了一个解决方案,我们只需要在包含模板之前添加一个用户不是管理员的检查。

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