参数$get行为奇怪



我收到以下错误"注意:未定义的索引:"在"名称"=> $_GET['通道']。

当有一个参数时,它可以工作并允许循环继续,但是当没有设置参数时,我会收到上面的错误。

如果我尝试 isset,它会删除错误,但如果设置了参数,则会停止 wordpress 循环继续。

我错过了什么?

<?php
$args = array (
'post_type' => 'abc_channels',
'name' => $_GET['channel'],
'post_status' => 'publish',
'posts_per_page' => 1,
);
$loop = new WP_query( $args );
// If we have live channels 
if (isset($_GET['channel'])) : 
if($loop->have_posts()): 
while($loop->have_posts()): $loop->the_post(); 
?>

感谢@aynber为我指出解决方案。

<?php $channelvalue = ""; //Initialization value; Examples
//"" When you want to append stuff later
//0  When you want to add numbers later
//isset()
$channelvalue = isset($_GET['channel']) ? $_GET['channel'] : '';
//empty()
$channelvalue = !empty($_GET['channel']) ? $_GET['channel'] : '';
?>
<?php
$args = array (
'post_type' => 'abc_channels',
'name' => $channelvalue,
'post_status' => 'publish',
'posts_per_page' => 1,
);
$loop = new WP_query( $args );
// If we have live channels 
if (isset($_GET['channel'])) : 
if($loop->have_posts()): 
while($loop->have_posts()): $loop->the_post(); 
?>

最新更新