【问题标题】:Wordpress Shortcode Displaying Above Widget Title显示在小部件标题上方的 Wordpress 简码
【发布时间】:2014-09-18 11:46:30
【问题描述】:

我在我的 functions.php 文件中添加了以下过滤器:

add_filter('widget_text', 'do_shortcode'); 

这已按预期在我的小部件中启用了短代码。

但是,短代码的内容现在显示在标题上方。我做错了什么?

这就是我通过简码回显的内容:

$events_msg = '<div>You have attended <br> <span class="big-num">'. $num_actual_events . '</span> / ' . $num_total_events . ' ' . $event_type . '</br>('. $num_required_events.' required)<br><br></div>';

echo $events_msg;

【问题讨论】:

  • 如果没有看到 html/css 的组成,这将很难分辨,但我会检查消息以查看边距是否设置为负值。
  • @Howlin 问题是它需要return 而不是echo 短代码的结果。

标签: shortcode wordpress


【解决方案1】:

问题是您在短代码中调用echo() 而不是return'ing 结果。如果您考虑调用函数的顺序,apply_filters( 'widget_text' ) 在实际小部件 HTML 输出之前运行,因此您的 echo 作为过滤器的一部分会导致它过早打印出来。更正此return 而不是echo 的结果。

add_filter('widget_text', 'do_shortcode'); 
add_shortcode('events', 'events_shortcode' );
function events_shortcode( $atts, $content=null ){
    // set default attribute values and extract to variables
    extract( shortcode_atts( array( 'attribute' => 'default_value' ), $atts ) );

    // populate your variables

    $events_msg = '<div>You have attended <br> <span class="big-num">'. $num_actual_events . '</span> / ' . $num_total_events . ' ' . $event_type . '</br>('. $num_required_events.' required)<br><br></div>';

    // return, don't echo
    return $events_msg;
}

【讨论】:

    【解决方案2】:

    当您将过滤器添加到您的functions.php时,添加以下内容可能也解决了问题

    add_filter( 'widget_text', 'shortcode_unautop' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-14
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2015-06-21
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多