$ get参数的奇怪行为。

时间:2017-08-14 16:10:12

标签: php arrays wordpress get

我收到以下错误"注意:未定义索引:" on' name' => $ _GET ['信道&#39]。

如果有一个参数可以运行并允许循环继续,但是当没有参数设置时,我得到上面的错误。

如果我尝试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(); 
    ?>

1 个答案:

答案 0 :(得分:0)

感谢@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(); 
    ?>