2017-06-17 23 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