【问题标题】:How to have the same shortcode multiple times on a page with different attribute values如何在具有不同属性值的页面上多次使用相同的简码
【发布时间】:2020-02-20 15:12:33
【问题描述】:

所以我基本上需要在同一页面上多次使用具有不同属性值的相同简码。

所以在我的页面上,我有以下简码:

[sponsors type="Platinum"]
[sponsors type="Premium"]
[sponsors type="Plus"]

然后我有多个 if 语句,它们都声明了以下内容:

if ( $atts['type'] == 'Platinum' ) {
?>
<h2><?php echo 'working'?></h2>
<?php
} else {}

if ( $atts['type'] == 'Premium' ) {
?>
<h2><?php echo 'working'?></h2>
<?php
} else {}

if ( $atts['type'] == 'Plus' ) {
?>
<h2><?php echo 'working'?></h2>
<?php
} else {}

但我注意到,当我这样做时,这些值只是结合起来,并不特定于每个短代码,因为当我转储 $atts['type'] 时,我得到了这个字符串 'Platinum Premium Plus'。

所以不确定我还能如何做到这一点?

【问题讨论】:

  • 我认为你需要一些关于这个的东西。你见过wordpress.stackexchange.com/questions/297003/…>吗?

标签: php wordpress shortcode wordpress-shortcode


【解决方案1】:

如何在具有不同属性值的页面上多次使用相同的简码

你是怎么做到的:

[sponsors type="Platinum"]
[sponsors type="Premium"]
[sponsors type="Plus"]

问题是短代码不是echo 的东西,它们必须return 一个带有您的代码的字符串。

像这样:

add_shortcode('sponsors', function($atts,$content){
    if ( $atts['type'] == 'Platinium' ) {
        $result = "<h2>Platinum working</h2>";
    }
    else if ( $atts['type'] == 'Premium' ) {
        $result = "<h2>Premium working</h2>";
    }
    else if ( $atts['type'] == 'Plus' ) {
        $result = "<h2>Plus working</h2>";
    }
    return $result;
});

【讨论】:

    猜你喜欢
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    相关资源
    最近更新 更多