[英]Undefined index and using isset
我在我的wordpress主題中有這個代碼,它給了我2個通知
我不是百分百肯定如何解決它。 我嘗試過使用isset()
但它不適用於我並且不斷出現不同的錯誤。
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
$array = array('template-homepage1.php', 'template-homepage2.php');
你也可以嘗試一下。
if(isset($_GET['post']))
{
$post_id = $_GET['post'];
}
if(isset($_POST['post_ID']))
{
$post_id = $_POST['post_ID'] ;
}
你需要首先初始化變量或使用empty()或isset()檢查php忽略通知
$post_id = '';$template_file='';
$post_id = (!empty($_GET['post']) ? $_GET['post'] : $_POST['post_ID']) ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
$array = array('template-homepage1.php', 'template-homepage2.php');
或者嘗試從php.ini中刪除php通知
您可以嘗試使用isset()
來檢查$_GET
變量和if else
在需要的地方分配值
if(isset($_GET['post'])
{
$post_id = $_GET['post'];
} else {
if(isset($_POST['post_ID'])
{
$post_id = $_POST['post_ID'];
} else {
$post_id = '';
}
}
$post_id = isset($_GET['post']) ? $_GET['post'] : isset($_POST['post_ID']) ? $_POST['post_ID'] : 0 ;
但這樣使用if可能更具可讀性
$post_id = 0;
if(isset($_GET['post'])) $post_id = $_GET['post'];
else if(isset($_POST['post_ID']) $post_id = $_POST['post_ID'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.