You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复基于Schema.org Person的短代码,使其内容显示在正确位置?

修复WordPress Schema Person短代码的位置问题

这个问题的核心原因很明确:WordPress短代码函数不能直接用echo输出内容,必须返回内容字符串。

当你用echo的时候,内容会在WordPress处理页面布局之前就被输出,所以会跑到页面顶部或者错误的位置,而不是短代码所在的地方。我们需要把你的代码改成返回拼接好的HTML字符串,让WordPress把它插入到正确的位置。

修复后的代码

function add_schema_person_shortcode() {
    // 拼接所有HTML内容到一个字符串里
    $output = '<div class="schema_person_microdata_container">';
    $output .= '<div itemscope itemtype="http://schema.org/Person">';
    $output .= '<h3>Personal Info</h3>';
    $output .= '</div>';
    $output .= '</div>';
    
    // 返回内容,而不是直接echo
    return $output;
}
// 注册短代码(你之前的代码里应该是没写完这部分)
add_shortcode('schema_person', 'add_schema_person_shortcode');

为什么这样改?

WordPress的短代码系统会调用你的函数,然后捕获它的返回值,再把这个值插入到短代码在文章/页面中被使用的位置。直接用echo会绕过这个流程,内容会在页面渲染的早期就被输出,自然位置不对。

可选优化:用Heredoc简化代码

如果后续要添加更多内容,用Heredoc语法会让代码更易读,不用反复拼接字符串:

function add_schema_person_shortcode() {
    $output = <<<HTML
<div class="schema_person_microdata_container">
    <div itemscope itemtype="http://schema.org/Person">
        <h3>Personal Info</h3>
    </div>
</div>
HTML;
    return $output;
}
add_shortcode('schema_person', 'add_schema_person_shortcode');

这样修改后,你在文章里使用[schema_person]短代码,内容就会显示在正确的位置了。

内容的提问来源于stack exchange,提问作者Atef Wagih

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:29:59