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 自定义WordPress短代码,无法用return替换echo_Php_Wordpress_Return_Echo_Shortcode - Fatal编程技术网

Php 自定义WordPress短代码,无法用return替换echo

Php 自定义WordPress短代码,无法用return替换echo,php,wordpress,return,echo,shortcode,Php,Wordpress,Return,Echo,Shortcode,这段代码在直接放入页面内容时效果非常好 <font color=#ff0000> <?php global $current_user; get_currentuserinfo(); echo $current_user->display_name . "\n"; echo 'Level: ' . $current_user->level . "\n <br>"; ?> </font> 结果显示“Sam等级

这段代码在直接放入页面内容时效果非常好

<font color=#ff0000> 
<?php global $current_user;
  get_currentuserinfo();

    echo $current_user->display_name . "\n"; 
    echo 'Level: ' . $current_user->level . "\n <br>";

?>
</font>
结果显示“Sam等级65”

然而,当我把它转换成一个短代码并尝试用return替换echo时,wordpress会中断,所有屏幕都会变成空白。 这是我尝试过的一个例子:

// Add Shortcode
function custom_shortcode() {

// Code
<font color=#ff0000> 
<?php global $current_user;
  get_currentuserinfo();

  $output = $current_user->display_name . "\n"; 
  $output .= 'Level: ' . $current_user->level . "\n <br>";

return $output;

?>
</font>
}
add_shortcode( 'userlevel', 'custom_shortcode' );
知道我哪里出错了吗


提前谢谢你

在需要PHP的地方使用HTML。这就是为什么你有一个空白屏幕

以下是我如何将其转换为短代码:

function wpse_shortcode_userlevel() {
    global $current_user;
    get_currentuserinfo();

    $output = '<span class="shortcode-user-level">';
    $output .= $current_user->display_name . "\n";
    $output .= "Level: {$current_user->level} \n <br>";
    $output .= '</span>';

    return $output;
}
add_shortcode( 'userlevel', 'wpse_shortcode_userlevel' );

你检查过错误日志了吗?它提供了任何线索吗?你能把错误日志添加到你的问题中吗?错误日志指向最后一行。它说‘调用未定义的函数add_shortcode等。我不知道这是怎么回事。仍然是空白。错误日志指向最后一行。add_shortcode显然是一个未定义的函数?请将错误日志[07-May-2014 00:41:26 UTC]PHP致命错误:调用未定义函数add_shortcode in/Users/Sam/Documents/Uni Work/New Mamp Test Site/wp includes/functions.PHP第4464行您添加的代码完全错误。删除添加到该文件中的所有代码。它应该放在活动主题文件夹中的functions.php中。转到/wp content/themes/your theme/functions.php并将其添加到那里。永远不要修改任何核心文件,如果它在可湿性粉剂管理或可湿性粉剂包括文件夹,它不应该被更改。哦,天哪,真是个傻瓜!现在一切都好了。内森,非常感谢你的耐心和帮助!
.shortcode-user-level {
    color: #ff0000;
}