【问题标题】:Plugin shortcode displays the template page in page editor (admin panel) Wordpress插件简码在页面编辑器(管理面板)Wordpress 中显示模板页面
【发布时间】:2018-04-13 06:29:52
【问题描述】:

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

add_shortcode('template', 'add_template');

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

开关($模板){

  case 'template1':
    include 'templates/template1.php';        
    break;

  case 'template2':
    include 'templates/template2.php';            
     break;

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

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 1970-01-01
      • 2011-06-17
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      相关资源
      最近更新 更多