[英]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.