Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-在短代码中包含属性_Php_Wordpress_Shortcode - Fatal编程技术网

PHP-在短代码中包含属性

PHP-在短代码中包含属性,php,wordpress,shortcode,Php,Wordpress,Shortcode,这似乎是Wordpress的问题,但我认为这是PHP特有的,而不是Wordpress。我想我的问题在//code块中。这是我的密码: function display_app_rating( $atts ) { // Attributes extract( shortcode_atts( array( 'app' => '', ), $atts ) ); // Code //return '"http://itunes.apple.com/lookup?

这似乎是Wordpress的问题,但我认为这是PHP特有的,而不是Wordpress。我想我的问题在
//code
块中。这是我的密码:

function display_app_rating( $atts ) {

// Attributes
extract( shortcode_atts(
    array(
        'app' => '',
    ), $atts )
);

// Code
//return '"http://itunes.apple.com/lookup?id='.($app).'"';
$json_url = '"http://itunes.apple.com/lookup?id='.($app).'"';
$json = file_get_contents($json_url);
$result = json_decode($json, TRUE);
foreach ($result['results'] as $key => $value) {
    return '<p class="appstore rating">Average rating '.$value['averageUserRating'].' out of 5 from '.$value['userRatingCount']. ' users.</p><p class="appstore price">Current Price '.$value['currency'].$value['price'].'</p>';
}
}
add_shortcode( 'apprating', 'display_app_rating' );

非常感谢您的帮助。

我认为您不希望在每次迭代后返回—将结果放入变量中,只返回一次

$result = json_decode($json, TRUE);
$block = '';
foreach ($result['results'] as $key => $value) {
    $block .='<p class="appstore rating">Average rating '.$value['averageUserRating'].' out of 5 from '.$value['userRatingCount']. ' users.</p><p class="appstore price">Current Price '.$value['currency'].$value['price'].'</p>';
}

return $block;
add_shortcode( 'myrating', 'display_my_app_rating_89' );
function display_my_app_rating_89( $atts, $content )
{
$args = shortcode_atts( 
    array(
        'app'   => '',
    ), 
    $atts
);

$result = 'No app id.';
$app = (int) $args['app'];

if( !empty( $_GET['app_id'] ) )
    $result = (int) $_GET['app_id'];
elseif( $app )
    $result = $app;

$json_url = "http://itunes.apple.com/lookup?id=$result";
//normal PHP version commented out and wordpress version activated - appears to be Wordpress best practice.
//$json = file_get_contents($json_url);
//wordpress version
$json = wp_remote_retrieve_body (wp_remote_get($json_url));
$result = json_decode($json, true);
$block = '';
foreach ($result['results'] as $key => $value) {
$block .='<span> ... whatever I wanted to return from JSON URL plus my HTML here ... </span>';
}
return $block;
}
$result=json\u decode($json,TRUE);
$block='';
foreach($result['results']作为$key=>$value){
$block.='

平均评级'.$value['averageUserRating'].'来自'.$value['userRatingCount'.'用户的5个。

当前价格'.$value['currency'.$value['price'.

'; } 返回$block;
我认为您不希望在每次迭代后返回—将结果放入变量,只返回一次

$result = json_decode($json, TRUE);
$block = '';
foreach ($result['results'] as $key => $value) {
    $block .='<p class="appstore rating">Average rating '.$value['averageUserRating'].' out of 5 from '.$value['userRatingCount']. ' users.</p><p class="appstore price">Current Price '.$value['currency'].$value['price'].'</p>';
}

return $block;
$result=json\u decode($json,TRUE);
$block='';
foreach($result['results']作为$key=>$value){
$block.='

平均评级'.$value['averageUserRating'].'来自'.$value['userRatingCount'.'用户的5个。

当前价格'.$value['currency'.$value['price'.

'; } 返回$block;
使用。要检查URL中的查询值,请使用

下面首先检查URL是否包含
?app_id=something
,如果是,结果将输出它。如果URL不包含它,则将使用shortcode属性
[apprating app=“something”]
。否则,它将打印一条错误消息:

add_shortcode( 'apprating', 'shortcode_so_25877611' );

function shortcode_so_25877611( $atts, $content )
{
    $args = shortcode_atts( 
        array(
            'app'   => '',
        ), 
        $atts
    );

    $result = 'No app id.';
    $app = (int) $args['app'];

    if( !empty( $_GET['app_id'] ) )
        $result = (int) $_GET['app_id'];
    elseif( $app )
        $result = $app;

    $json_url = "http://itunes.apple.com/lookup?id=$result";

    return $return;
}
post上的测试短代码如下所示,然后将查询变量附加到URL:

id为:[通知应用程序=“1234”] 没有:[告知] PS:as,您的
返回必须在
foreach
循环之外。

使用。要检查URL中的查询值,请使用

下面首先检查URL是否包含
?app_id=something
,如果是,结果将输出它。如果URL不包含它,则将使用shortcode属性
[apprating app=“something”]
。否则,它将打印一条错误消息:

add_shortcode( 'apprating', 'shortcode_so_25877611' );

function shortcode_so_25877611( $atts, $content )
{
    $args = shortcode_atts( 
        array(
            'app'   => '',
        ), 
        $atts
    );

    $result = 'No app id.';
    $app = (int) $args['app'];

    if( !empty( $_GET['app_id'] ) )
        $result = (int) $_GET['app_id'];
    elseif( $app )
        $result = $app;

    $json_url = "http://itunes.apple.com/lookup?id=$result";

    return $return;
}
post上的测试短代码如下所示,然后将查询变量附加到URL:

id为:[通知应用程序=“1234”] 没有:[告知]

PS:as,您的
返回必须在
foreach
循环之外。

这是我最后使用的代码。这是各种反应的混合体。看起来很好用。感谢您的指导

add_shortcode( 'myrating', 'display_my_app_rating_89' );
function display_my_app_rating_89( $atts, $content )
{
$args = shortcode_atts( 
    array(
        'app'   => '',
    ), 
    $atts
);

$result = 'No app id.';
$app = (int) $args['app'];

if( !empty( $_GET['app_id'] ) )
    $result = (int) $_GET['app_id'];
elseif( $app )
    $result = $app;

$json_url = "http://itunes.apple.com/lookup?id=$result";
//normal PHP version commented out and wordpress version activated - appears to be Wordpress best practice.
//$json = file_get_contents($json_url);
//wordpress version
$json = wp_remote_retrieve_body (wp_remote_get($json_url));
$result = json_decode($json, true);
$block = '';
foreach ($result['results'] as $key => $value) {
$block .='<span> ... whatever I wanted to return from JSON URL plus my HTML here ... </span>';
}
return $block;
}
add_shortcode('myrating','display_my_app_rating_89');
功能显示\我的\应用\评级\ 89($atts,$content)
{
$args=短码_附件(
排列(
'应用程序'=>'',
), 
$atts
);
$result='没有应用程序id';
$app=(int)$args['app'];
如果(!empty($\u GET['app\u id']))
$result=(int)$\u GET['app\u id'];
elseif($app)
$result=$app;
$json_url=”http://itunes.apple.com/lookup?id=$result”;
//普通PHP版本被注释掉,wordpress版本被激活——这似乎是wordpress的最佳实践。
//$json=file\u get\u contents($json\u url);
//wordpress版本
$json=wp_remote_retrieve_body(wp_remote_get($json_url));
$result=json_decode($json,true);
$block='';
foreach($result['results']作为$key=>$value){
$block.='…我想从JSON URL加上我的HTML返回的任何内容;
}
返回$block;
}

这是我最后使用的代码。这是各种反应的混合体。看起来很好用。感谢您的指导

add_shortcode( 'myrating', 'display_my_app_rating_89' );
function display_my_app_rating_89( $atts, $content )
{
$args = shortcode_atts( 
    array(
        'app'   => '',
    ), 
    $atts
);

$result = 'No app id.';
$app = (int) $args['app'];

if( !empty( $_GET['app_id'] ) )
    $result = (int) $_GET['app_id'];
elseif( $app )
    $result = $app;

$json_url = "http://itunes.apple.com/lookup?id=$result";
//normal PHP version commented out and wordpress version activated - appears to be Wordpress best practice.
//$json = file_get_contents($json_url);
//wordpress version
$json = wp_remote_retrieve_body (wp_remote_get($json_url));
$result = json_decode($json, true);
$block = '';
foreach ($result['results'] as $key => $value) {
$block .='<span> ... whatever I wanted to return from JSON URL plus my HTML here ... </span>';
}
return $block;
}
add_shortcode('myrating','display_my_app_rating_89');
功能显示\我的\应用\评级\ 89($atts,$content)
{
$args=短码_附件(
排列(
'应用程序'=>'',
), 
$atts
);
$result='没有应用程序id';
$app=(int)$args['app'];
如果(!empty($\u GET['app\u id']))
$result=(int)$\u GET['app\u id'];
elseif($app)
$result=$app;
$json_url=”http://itunes.apple.com/lookup?id=$result”;
//普通PHP版本被注释掉,wordpress版本被激活——这似乎是wordpress的最佳实践。
//$json=file\u get\u contents($json\u url);
//wordpress版本
$json=wp_remote_retrieve_body(wp_remote_get($json_url));
$result=json_decode($json,true);
$block='';
foreach($result['results']作为$key=>$value){
$block.='…我想从JSON URL加上我的HTML返回的任何内容;
}
返回$block;
}

$json_url=”“应该可以工作。字符串周围有单引号,它不内联变量。我没有使用wordpress的经验,你能验证一下$app是从摘录中正确设置的吗?嗨,Hammerstein,不幸的是,尝试过之后它就不起作用了。当我简单地返回显示的那一行时,它会将URL与引号内的变量正确地内联起来,作为URL的一部分。$json_URL=”“应该可以工作。字符串周围有单引号,它不内联变量。我没有使用wordpress的经验,你能验证一下$app是从摘录中正确设置的吗?嗨,Hammerstein,不幸的是,尝试过之后它就不起作用了。当我简单地返回显示的那一行时,它会正确地将URL与引用中的变量作为URL的一部分进行内联。谢谢你,亚当,我可能会误解,但我感谢你的输入,毫无疑问,你的代码将在该块中改进我的代码。但是我遇到的问题是如何将我的$app变量(由快捷码定义)放入URL中,这在之前发生过?听起来你应该将这个函数拆分。。。尝试将其分为get_app_rating()、set_app_url()、display_app_rating()或类似内容。我想在你们之间,这一切都对我有用。非常感谢。我添加了你的块返回想法,但不需要创建单独的函数。右上=)我建议个别函数的原因是它使测试变得更容易-一些考虑向前移动。谢谢亚当,我可能是误解,但我欣赏你的输入,毫无疑问,你的代码将改善我在那个块。我遇到的问题是将我的$app变量(由短代码定义)放入URL中,这在这之前发生过吗