在Wordpress上的函数中使用短代码[复制]

问题描述 投票:-1回答:2

这个问题在这里已有答案:

我在wordpress网站中有一个函数文件,其中包含在呈现网站上的页面时由相关钩子调用的各种函数。

一个特定的功能工作正常但我现在已经在函数的代码中添加了与Wordpress插件“Collapse-O-Matic”相关的短代码。呈现页面时,短代码在方括号中显示为短代码本身!我认为有些东西我不了解如何渲染短代码的结果,并想知道是否有人能够向我解释如何正确地做到这一点。

短代码是[expand title="Open" swaptitle="Close"]Target Content[/expand],我把它放在这个函数中如下(请注意这不是函数内的所有代码):

<ul class="admin">
                    <?php
                    // loop through rows (sub repeater)
                    while( have_rows('item_list_details') ): the_row() 
                        // display each item as a list
                        ?>
                            <?php

                                $date = new DateTime(get_sub_field('date'));
                                $now = new DateTime(Date('Y-m-d'));
                                $diff = $now->diff($date);

                                if ($diff->days > $latest):    //Use $diff->days and not $diff->d
                            ?>
                                <li class='research'>
                            <?php else: ?>
                                <li class='researchLatest'>
                            <?php endif;?>
                           <div class='itemTitle'>
                                    <?php $link = get_sub_field('link_url'); if( $link ): ?>
                                    <a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>" title="<?php echo $link['title']; ?>">
                                    <?php endif; ?>
                                    <?php the_sub_field('link_name'); ?>
                                    <?php $link = get_sub_field('link_url'); if( $link ): ?>
                                    </a>
                                    <?php endif; ?><p class='alert'> - <strong>NEW</strong></p>
                                </div>
                                <br/>
                                <div class="itemDescription">
                                    [expand title="Open" swaptitle="Close"]<?php the_sub_field('link_description'); ?>[/expand]
                                </div>
                            </li>   
                    <?php endwhile; ?>
                    </ul>`

正如您所看到的,在短代码(<?php the_sub_field('link_description'); ?>)中有一个php表达式,但希望它仍然可以正确地进行渲染。

php wordpress shortcode wordpress-shortcode
2个回答
1
投票

你正在寻找的功能是do_shortcode()

一般来说,这只是一个问题:

echo do_shortcode('[shortcode]whatever[/shortcode]');

此外,您正在使用来自ACF的the_sub_field(),它直接输出并且不返回任何内容,因此您无法将其结果传递给do_shortcode()

您可以使用get_sub_field(),捕获输出,然后将所有内容传递给do_shortcode()

例如:

$linkDescription   = get_sub_field('link_description');
$renderedShortcode = do_shortcode("[expand title="Open" swaptitle="Close"]$linkDescription[/expand]");

echo $renderedShortcode;

如果您在使用前需要检查短代码是否存在,则可以使用shortcode_exists()

EG


if (shortcode_exists('expand')) {
    echo do_shortcode("[expand title="Open" swaptitle="Close"]$linkDescription[/expand]");
}
else {
    echo $linkDescription;
}

文档:


0
投票

你的代码里面你必须使用wordpress函数do_shortcode($ string)

<?php echo do_shortcode("[expand title=\"Open\" swaptitle=\"Close\"]".get_sub_field('link_description')."[/expand]") ?>

你实际上可以将混有html和shortcodes的整个块传递给这个函数,它将返回一个字符串,其中包含所有呈现的内容。

这显然也有效

$output=do_shortcode("[expand title=\"Open\" swaptitle=\"Close\"]".get_sub_field('link_description')."[/expand]");

编辑:根据yivi的输入修复get_sub_field

© www.soinside.com 2019 - 2024. All rights reserved.