【问题标题】:How to make nested shortcodes work in WordPress?如何使嵌套的短代码在 WordPress 中工作?
【发布时间】:2014-01-17 10:36:43
【问题描述】:

我对 WordPress 相当胜任,但我正试图让事情变得更复杂一些以扩展自己,并且我正在尝试创建自定义短代码。我已经成功地创建了两个单独的短代码; 1) features_box 2) awesome_font_icon.

这两个短代码可以完美地作为单个实体工作,但在尝试嵌套短代码时,例如)[featured_box][icon type="fa-bullhorn"][/featured_box] [icon type="fa-bullhorn"] 仅显示该文本,而不是所需的 Awesome Font Icon。

我知道您必须将 do_shortcode() 添加到脚本中,但我不完全确定将 do_shortcode() 放在哪里以及放在哪个短代码中,无论我将它放在 features_box 还是 awesome_font 短代码中。

我把这两个脚本都放在下面了。

特色盒子 PHP:

function featured_box($atts, $content = null) {
$sliderrandomid = rand();
extract(shortcode_atts(array(
"title" => '',
'img'  => '',
'pos' => '',
), $atts));
ob_start();
?>

<div class="featured-box <?php if($pos) echo 'pos-'.$pos; ?>">
<img class="featured-img" src="<?php echo $img; ?>">
<h4><span><?php echo $title; ?></span></h4>
<p><?php echo $content; ?></p>
</div>

<?php
$content = ob_get_contents();
ob_end_clean();
return $content;
}


add_shortcode("featured_box", "featured_box");

真棒字体简码:

add_action( 'wp_enqueue_scripts', 'load_fontawesome_style', 999 );
function load_fontawesome_style() {
  wp_register_style( 'font-awesome', 'http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css' );
  wp_enqueue_style( 'font-awesome' );
}



function addscFontAwesome( $atts ) {
extract( shortcode_atts( array( 'type' => '', 'size' => ''), $atts ) );

  $theAwesomeFont = '<li class="fa '.sanitize_html_class($type).' '.sanitize_html_class($size).'"></li>';

return $theAwesomeFont;
}

add_shortcode('icon', 'addscFontAwesome');

我浏览了整个网络并找到了各种不同的教程等,但到目前为止还没有成功。正如我所说,一切都很完美,只是没有在一起。

我很欣赏嵌套的短代码已经被覆盖到死;我发现的所有内容都非常通用,我认为我不是那种擅长 PHP 的向导来了解将它放在哪里。

【问题讨论】:

    标签: php wordpress nested shortcode


    【解决方案1】:

    你的想法是对的。我相信您会将do_shortcode(); 添加到featured_box 函数中。像这样:

    function featured_box($atts, $content = null) {
        $sliderrandomid = rand();
        extract(shortcode_atts(array(
            "title" => '',
            'img'  => '',
            'pos' => '',
        ), $atts));
        ob_start();
        ?>
    
        <div class="featured-box <?php if($pos) echo 'pos-'.$pos; ?>">
        <img class="featured-img" src="<?php echo $img; ?>">
        <h4><span><?php echo $title; ?></span></h4>
        // HERE
        <p><?php echo do_shortcode($content); ?></p> // HERE
        </div>
    
    <?php
        $content = ob_get_contents();
        ob_end_clean();
        return $content;
    }
    

    在回显内容时注意do_shortcode($content); 部分。

    整洁的代码

    我冒昧地整理了一下代码,以使用输出缓冲来保存。只需将 HTML 创建为字符串并将变量/函数输出连接到其中。真的只是个人喜好,所以取决于你! :)

    我还建议使用带有&lt;i class="fa fa-icon"&gt;&lt;/i&gt; html 标记的font-awesome,而不是&lt;li class="fa fa-icon"&gt;&lt;/li&gt; 标记。在&lt;ul&gt; 标记之外使用&lt;li&gt; 标记会导致无效的html,而简单的&lt;i&gt; 标记不会。

    function featured_box($atts, $content = null) {
        $sliderrandomid = rand();
        extract(shortcode_atts(array(
            "title" => '',
            'img'  => '',
            'pos' => '',
        ), $atts));
    
        $content = '
            <div class="featured-box ' . ($pos ? echo 'pos-'.$pos : null) . '">
                <img class="featured-img" src="' . $img . '">
                <h4><span>' . $title .'</span></h4>
                <p>' . do_shortcode($content) . '</p>
            </div>
        ';
    
        return $content;
    }
    
    function addscFontAwesome( $atts ) {
        extract( shortcode_atts( array( 'type' => '', 'size' => ''), $atts ) );
    
        // Note the <i> tag instead of <li>        
        $theAwesomeFont = '<i class="fa '.sanitize_html_class($type).' '.sanitize_html_class($size).'"></i>';
    
        return $theAwesomeFont;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      相关资源
      最近更新 更多