【问题标题】:Read Serialized Array as Shortcode Argument将序列化数组读取为简码参数
【发布时间】:2018-02-06 03:11:57
【问题描述】:

我在 WordPress 中使用带有高级自定义字段插件的事件管理器插件。

在我的活动帖子类型中,我使用 ACF 添加了一个图片库,并希望在活动管理器创建的活动列表页面中显示第一张图片。我可以在事件列表中执行此操作的唯一方法是创建一个短代码,该代码将读取从自定义字段返回的序列化数组,该字段通常类似于 a:1:{i:0;s:4:"6903";}

function unseralLink( $atts ) {
    $atts = shortcode_atts(
        array(
            'id' => '',
        ), $atts, 'unseralizeLink');
    if($atts['id']!='')
        {
        $mydata =  unserialize($atts['id']);
        $url = wp_get_attachment_image_url($mydata[0]);
        return "<img src=\"".$url."\" alt=\"\" class=\"attachment-thumbnail size-thumbnail\" />";
    }
}
add_shortcode( 'unseralizeLink', 'unseralLink' );

我用[unseralizeLink id="#_ATT{gallery}"] 调用简码。但是什么都没有返回。

我真正需要帮助的是读取序列化数组作为简码参数并将其存储到函数内的局部变量中。在那之后,我应该会没事的。

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    经过大量试验,我找到了解决方案。

    function eventImgURL( $atts ) {
    shortcode_atts(
        array(
            'id' => '',
        ), $atts );
        $myvar = unserialize($atts[id]);
        return wp_get_attachment_image($myvar[0]);
    
    }
    add_shortcode( 'unseralizelink', 'eventImgURL' );
    

    部分问题是我在使用双引号时在短代码中使用双引号调用序列化数组。

    [unseralizelink id='#_ATT{gallery}']
    

    【讨论】:

      【解决方案2】:

      我刚遇到同样的问题,提问者发布的解决方案不起作用。这是我的,工作得很好,下一个有同样问题的人:

      function eventImgURL( $atts ) {
          $atts = shortcode_atts( array( 'id' => '',), $atts, 'unserialize-link' );
          return wp_get_attachment_image($atts['id']);
      }
      add_shortcode( 'unserialize-link', 'eventImgURL' );
      

      然后在你的 HTML 模板中调用它,如下所示:

      <p>[unserialize-link id='#_ATT{logo}']</p>
      

      请参阅documentation on shortcodes from the WordPress Codex 了解更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-04
        • 1970-01-01
        • 1970-01-01
        • 2019-08-31
        • 2019-08-10
        • 2011-09-21
        • 1970-01-01
        相关资源
        最近更新 更多