简体   繁体   English

用do_shortcode()在每个短代码周围包装HTML

[英]Wrap HTML around each shortcode with do_shortcode()

I have a page that has multiple shortcodes on it. 我的页面上有多个短代码。 I would like to have the content from each shortcode wrapped with an HTML tag. 我想用HTML标记包装每个简码中的内容。 I have the following code that almost works but it wraps all of the shortcodes in one div. 我有以下几乎可以正常工作的代码,但是它将所有短代码包装在一个div中。

<?php $sidebarBlocks = get_post_meta($post->ID, "page_sidebarhubspot", true); ?>
<?php echo do_shortcode('<div>'.$sidebarBlocks.'</div>'); ?>

This outputs.. 输出..

<div>
   <p>Content from shortcode1</p>
   <p>Content from shortcode2</p>
   <p>Content from shortcode3</p>
</div>

What I want is this.. 我想要的是这个

<div>
   <p>Content from shortcode1</p>
</div>

<div>
   <p>Content from shortcode2</p>
</div>

<div>
   <p>Content from shortcode3</p>
</div>

How can I accomplish this? 我该怎么做? Thanks! 谢谢!

That's a good question - I don't think it's currently possible without hacking around. 这是一个好问题-我认为如果不进行黑客入侵,目前不可能实现。 Here's my go at that: 这是我的努力:

<?php 
function my_override_shortcodes() {
    global $shortcode_tags, $_shortcode_tags;

    // Let's make a back-up of the shortcodes
    $_shortcode_tags = $shortcode_tags;

    // Add any shortcode tags that we shouldn't touch here
    $disabled_tags = array( 'gallery' );

    foreach ( $shortcode_tags as $tag => $cb ) {
        if ( in_array( $tag, $disabled_tags ) ) {
            continue;
        }
        // Overwrite the callback function
        $shortcode_tags[ $tag ] = 'my_wrap_shortcode_in_div';
    }
}
add_action( 'init', 'my_override_shortcodes', 9999 );

// Wrap the output of a shortcode in a div with class "i-wrap-you"
// The original callback is called from the $_shortcode_tags array
function my_wrap_shortcode_in_div( $attr, $content = null, $tag ) {
    global $_shortcode_tags;
    return '<div class="i-wrap-you">' . call_user_func( $_shortcode_tags[ $tag ], $attr, $content, $tag ) . '</div>';
}

So what happens here is that on init we copy all of the registered shortcodes and overwrite their callback function with our own function. 因此,这里发生的是,在init我们复制了所有已注册的简码,并用我们自己的函数覆盖了它们的回调函数。

That function on the other hands when called returns an opening div tag, followed by the output of the original callback function and then a closing div tag. 另一方面,该函数在调用时将返回一个div标签开始,然后是原始回调函数的输出,然后是一个div标签结束。

If you want to override the shortcodes just for that do_shortcode call, you can do something like this: 如果您只想为该do_shortcode调用覆盖短do_shortcode ,则可以执行以下操作:

function my_restore_shortcodes() {
    global $shortcode_tags, $_shortcode_tags;

    // Restore the original callbacks
    if ( isset( $_shortcode_tags ) ) {
        $shortcode_tags = $_shortcode_tags;
    }
}

And in your code do this: 然后在您的代码中执行以下操作:

$sidebarBlocks = get_post_meta( $post->ID, "page_sidebarhubspot", true );

my_override_shortcodes();

echo do_shortcode('<div>'.$sidebarBlocks.'</div>');

my_restore_shortcodes();

Of course if you use the second method, don't forget to remove the line 当然,如果您使用第二种方法,请不要忘记删除该行

add_action( 'init', 'my_override_shortcodes', 10 );

As a bonus in the my_override_shortcodes() function you can specify shortcodes that will not be overwritten(the $disabled_tags array). 作为my_override_shortcodes()函数的一项奖励,您可以指定不会被覆盖的短代码( $disabled_tags数组)。

You can try like this [ You can remove the htmlentities() , I have used it for illustration purposes , just use plain echo ] 您可以尝试这样[ 您可以删除 htmlentities()我已将其用于说明目的,仅使用普通 echo ]

<?php $sidebarBlocks = get_post_meta($post->ID, "page_sidebarhubspot", true); ?>
<?php 
$sidebarBlocks=str_replace("<p>","<div><p>",$sidebarBlocks);
$sidebarBlocks=str_replace("</p>","</p></div>",$sidebarBlocks);
echo htmlentities($str);
?>

OUTPUT : 输出:

<div><p>Content from shortcode1</p></div>
<div><p>Content from shortcode2</p></div>
<div><p>Content from shortcode3</p></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM