【问题标题】:How to style a shortcode based on acf如何根据 acf 设置简码样式
【发布时间】:2019-12-08 22:49:06
【问题描述】:

我想将我页面中的 acf 字段的输出与我的简码结合起来。文本应使用通过 acf 字段设置的颜色加下划线。

我尝试调用字段颜色并通过内联样式设置文本装饰。但这不起作用。任何想法我做错了什么?

function quote_func($atts, $content = null){
 $color = get_field('color');
    $output = '<div>';
    $output .= '<span style="text-decoration-color:' . echo the_field('color'); . '">' . $content . '</span>';
    $output .= '</div>';

    return $output;
}
add_shortcode( 'quote', 'quote_func' );

【问题讨论】:

    标签: html css wordpress shortcode


    【解决方案1】:

    您应该回显您在函数开头设置的变量。

    function quote_func($atts, $content = null){
       $color = get_field('color');
       $output = '<div>';
       $output .= '<span style="text-decoration-color:' . $color . '">' . $content . '</span>';
       $output .= '</div>';
    
       return $output;
    } 
    add_shortcode( 'quote', 'quote_func' );
    

    【讨论】:

      【解决方案2】:

      get_field('color') 如果您不在帖子中,则不足以获取该值,您需要第二个参数。您使用的是简码,那么您需要使用:

      get_field('color', $postId);

      要从短代码中获取帖子的 ID,您可以使用:

      global $post;
      $postId = $post->ID;
      

      如果您为每个帖子使用相同的颜色,您可能会有选项页面,在这种情况下您需要使用:

      get_field('color', 'option');
      

      【讨论】:

        猜你喜欢
        • 2012-03-07
        • 2018-04-04
        • 2019-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-10
        • 2013-04-03
        • 2020-02-24
        相关资源
        最近更新 更多