【问题标题】:Using Shortcodes within functions on Wordpress [duplicate]在 Wordpress 的函数中使用简码 [重复]
【发布时间】:2019-07-05 00:38:59
【问题描述】:

我在 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>`

如您所见,短代码 (&lt;?php the_sub_field('link_description'); ?&gt;) 中有一个 php 表达式,但希望它仍然可以正确呈现。

【问题讨论】:

    标签: php wordpress shortcode wordpress-shortcode


    【解决方案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()

    例如

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

    文档:

    【讨论】:

      【解决方案2】:

      在您的代码中,您必须使用 wordpress 函数 do_shortcode( $string )

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

      您实际上可以将混合了 html 和简码的整个块传递给这个函数,它会返回一个包含所有内容的字符串。

      这显然也有效

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

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

      【讨论】:

      • 非常感谢。如果我想首先检查短代码(即插件)是否可用,如果没有抛出一条消息说“短代码不可用”,我该怎么做?
      猜你喜欢
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2011-11-25
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多