2017-06-17 49 views
1

我在拉JSON數據並創建我的頁面WordPress的 - 從網頁發送變量主題功能

我的問題是如何從通過RID =值使用簡碼的主題函數文件中的函數使用短代碼 [json_employees]並將RID發送回函數的頁面?

function foobar2_func($atts){ 
ob_start(); 
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965'; 
$data = file_get_contents($url); 
$items = str_replace('<p></p>', '', $items); 
$items = json_decode($data, true); 
?> 
add_shortcode('json_employees', 'foobar2_func'); 
+0

你有沒有試着用'shortcode_atts'? – vel

回答

0

你的簡碼應該是

[json_employees RID='17965'] 

和簡碼功能是

function foobar2_func($atts){ 
    ob_start(); 
    $atts = shortcode_atts(
    array(
     'RID' => 'id', 
    ), $atts); 

    $url = 'https://jdublu.com/api/wrsc/json_employee.php?RID='.esc_attr($atts['RID']); 
    $data = file_get_contents($url); 
    $items = str_replace('<p></p>', '', $items); 
    $items = json_decode($data, true); 
    return $items; 
} 
add_shortcode('json_employees', 'foobar2_func'); 
+0

謝謝,但沒有得到函數中的RID,還有什麼可能? – Mariano

+0

打印此變量$ atts並檢查 – vel

+0

我做過並顯示「數組」並且沒有其他東西 – Mariano

0
function foobar2_func($atts){ 
     ob_start(); 
     $atts = shortcode_atts(
     array(
      'RID' => 'id', 
     ), $atts); 

     $url = 'https://jdublu.com/api/wrsc/json_employee.php?RID='.esc_attr($atts['RID']); 
     $data = file_get_contents($url); 
     $items = str_replace('<p></p>', '', $items); 
     $items = json_decode($data, true); 
     return $items; 
     print_r ($atts); 
    } 
    add_shortcode('json_employees', 'foobar2_func'); 

    and in the wordpress page 
    [json_employees RID='17965'] 

here is the page to test it http://bahiacr.com/test_json/ 
+0

function foobar2_func($ atts){ ob_start(); $ atts = shortcode_atts( array( 'RID'=>'id', ),$ atts); $ url ='https://jdublu.com/api/wrsc/json_employee.php?RID='.esc_attr($atts['RID']); $ data = file_get_contents($ url); $ items = str_replace('

','',$ items); $ items = json_decode($ data,true); 返回$ items; print_r($ atts); } add_shortcode('json_employees','foobar2_func'); and in wordpress page [json_employees RID ='17965'] http://bahiacr.com/test_json/ – Mariano