【问题标题】:WordPress Shortcode with fallback带有后备功能的 WordPress 简码
【发布时间】:2020-07-14 14:37:00
【问题描述】:

我正在构建一个使用geoservices web service 的简单插件,我正在尝试根据其位置动态更改 WordPress 页面上的内容。我让它有点工作,但我的问题是它同时返回特定位置的文本和默认值。我知道这是因为我不止一次使用简码实例,但我不知道如何将其更改为仅显示特定于位置的内容,如果位置未设置或与简码参数不匹配,则回退到默认一个。我不想将“默认”添加为简码参数,因为它可能包含 HTML 或其他内容。

这是我的简码示例:

[geo city="Orlando"]<a href="#">555-123-6349</a>[/geo][geo city="Raleigh"]<a href="#">919-999-9999</a>[/geo][geo city="Default"]<a href="#">Default text here</a>[/geo]

因此,根据上述情况,如果用户来自奥兰多,所需的结果将显示奥兰多的电话号码,如果用户来自罗利,则显示罗利号码。否则,如果他们不是来自这些地方的任何一个,它将使用默认值。

这是我的简码:

function geo_services( $atts , $content = null ) {

    // Attributes
    extract(shortcode_atts(array(
      'city' => '',
      'state' => '',
   ), $atts));

    require_once('geoplugin.class.php');

    $geoplugin = new geoPlugin();

    $geoplugin->locate();

    if($city === $geoplugin->city){
        return $content;
    } elseif ($state === $geoplugin->region){
        return $content;
    } elseif ($city === 'Default') {
        return $content;
    }


}
add_shortcode( 'geo', 'geo_services' );

当我使用上面的示例短代码时,会发生以下情况:

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    我相信您可能误解了短代码在 WP 中的工作原理。在您的示例中,您向内容添加了 3 个简码。这些短代码中的每一个都将运行。不是其中之一。这样做,

    [geo city="Orlando"]<a href="#">555-123-6349</a>[/geo][geo city="Raleigh"]<a href="#">919-999-9999</a>[/geo][geo city="Default"]<a href="#">Default text here</a>[/geo]
    

    表示将调用和评估其中的每一个。 $geoplugin-&gt;city 总是会返回用户所在的城市,无论您提供什么属性。而且由于您在所有情况下都返回$content,因此它总是会吐出您在短代码中添加的内容。这就是您看到所有 3 个响应的原因。

    相反,我会尝试以下方法。如果您的目标是根据用户所在的城市输出内容,那么您实际上不需要为短代码提供属性。请参阅以下示例:

    //in your post/page content, simply use the shortcode geo
    [geo]
    
    //your function should be
    function geo_services( $atts , $content = null ) {
    
        //
        require_once('geoplugin.class.php');
    
        //
        $geoplugin = new geoPlugin();
        $geoplugin->locate();
    
        //
        switch( $geoplugin->city ) {
            case 'Orlando':
                    return '<a href="#">555-123-6349</a>';
                break;
            case 'Raleigh':
                    return '<a href="#">919-999-9999</a>';
                break;
            default:
                    return '<a href="#">Default text here</a>';
                break;
        }
    
    }
    add_shortcode( 'geo', 'geo_services' );
    

    【讨论】:

    • 谢谢,我考虑过这种方法,但问题是内容可能会从所见即所得的内容编辑器中设置,所以我不能真正以编程方式设置内容。我需要返回 $content
    • 所以您只想在城市与属性中的内容匹配时才返回提供的内容?你想检查多个城市?你总是可以让你的简码返回“”,这意味着它不会吐出任何东西。但是,如果您总是从简码返回内容,并且每页有多个简码,那么您总是会看到多个内容。
    【解决方案2】:

    基于 OP cmets 提供另一个答案。如果您确实需要通过所见即所得的方式管理内容,则可以将每个城市的内容作为属性提供。

    //add shortcode to post/page content
    [geo orlando="555-123-6349" raleigh="919-999-9999" default="Custom default text here"]
    
    //your function should be
    function geo_services( $atts , $content = null ) {
    
        //don't use extract since we expect X number of atts now
        $atts = shortcode_atts(array(
            'default' => 'Default text here'
        ), $atts);
    
        //
        require_once('geoplugin.class.php');
    
        //
        $geoplugin = new geoPlugin();
        $geoplugin->locate();
    
        //was the city provided as an attribute?
        if( isset($atts[ strtolower($geoplugin->city) ]) ) {
            return $atts[ strtolower($geoplugin->city) ];
        }else {
            return $atts['default'];
        }
    
    }
    
    add_shortcode( 'geo', 'geo_services' );
    

    您可能需要对内容的 HTML 部分进行创作,但现在您可以在短代码本身中包含 X 个城市及其自定义内容。如果未提供或不匹配城市,它将回退到默认值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      相关资源
      最近更新 更多