2017-08-14 12 views
0

次のエラーが表示されます。 "通知:未定義のインデックス:" 'name' => $ _GET ['channel']。

パラメータがあるとループが進行しますが、パラメータが設定されていない場合は上記のエラーが表示されます。

私はissetを試すと、エラーを取り除きますが、パラメータが設定されていれば、ワードプレスループが進行しなくなります。

私には何が欠けていますか?

<?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(); 
    ?> 
+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(); 
    ?> 
関連する問題