【问题标题】:Shortcode return jumps outside div in wordpress简码返回在wordpress中跳到div之外
【发布时间】:2013-07-18 17:06:48
【问题描述】:

问题是the_ratigs()跳出<div id="wpratings">

function wpratings( $atts, $content = null ) {
    return '<div id="wpratings"><b>Rating: </b>' . the_ratings() . '</div>';
}

add_shortcode("wpratings", "wpratings");

短代码输出示例:

5.00 <<< this is output of the_ratings()
Here is the post content
Rating: <<< this is place of <div id="wpratings">

【问题讨论】:

    标签: html wordpress shortcode


    【解决方案1】:

    通常,任何以the_* 开头的函数都会得到print的结果。你需要的是一个等效的函数get_the_ratings()

    在 WP 中,我们有 the_titleget_the_titlethe_contentget_the_content。这就是它失败的原因,短代码必须是 return 值,而不是 print 它们。

    你没有提到这是什么插件。如果是WP-PostRatings,则没有get_the_rating,但你有以下内容:

    the_ratings($start_tag = 'div', $custom_id = 0, $display = true)
    

    解决方案:将$display 传递为false

    return '<div id="wpratings"><b>Rating: </b>' . the_ratings('div',0,false) . '</div>';
    

    【讨论】:

    • 我明白了。 get_the_ratings() 是未定义的函数。
    【解决方案2】:

    这有什么不同吗?

    function wpratings( $atts, $content = null ) {
        $content = the_ratings();
        return '<div id="wpratings"><b>Rating: </b>' . $content . '</div>';
    }
    
    add_shortcode("wpratings", "wpratings");
    

    $content 是放在[shortcode]content[/shortcode] 标签之间的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多