Php 在Wordpress上的函数中使用短代码
我在wordpress网站中有一个函数文件,其中包含各种函数,当呈现网站上的页面时,它们的相关钩子会调用这些函数 一个特定的函数工作正常,但我现在在函数的代码中添加了一个与Wordpress插件“Collapse-O-Matic”相关的短代码。当呈现页面时,短代码在方括号中显示为短代码本身!我想对于如何呈现一个短代码的结果,我有一些不理解的地方,我想知道是否有人能够向我解释如何正确地实现这一点 短代码是Php 在Wordpress上的函数中使用短代码,php,wordpress,shortcode,wordpress-shortcode,Php,Wordpress,Shortcode,Wordpress Shortcode,我在wordpress网站中有一个函数文件,其中包含各种函数,当呈现网站上的页面时,它们的相关钩子会调用这些函数 一个特定的函数工作正常,但我现在在函数的代码中添加了一个与Wordpress插件“Collapse-O-Matic”相关的短代码。当呈现页面时,短代码在方括号中显示为短代码本身!我想对于如何呈现一个短代码的结果,我有一些不理解的地方,我想知道是否有人能够向我解释如何正确地实现这一点 短代码是[expand title=“Open”swaptitle=“Close”]目标内容[/exp
[expand title=“Open”swaptitle=“Close”]目标内容[/expand]
,我将它放在这个函数中,如下所示(请注意,这不是函数中的全部代码):
-
-
-新建
[expand title=“Open”swaptitle=“Close”][/expand]
`
正如您所看到的,在短代码(
)中有一个php表达式,但希望仍然可以正确地进行渲染。在代码中,您必须使用wordpress函数do\u shortcode($string)
编辑:根据yivi的输入修复了get_sub_字段您要查找的函数是
do_shortcode()
一般来说,这只是一个简单的问题:
echo do_shortcode(“[shortcode]which[/shortcode]”);
此外,您使用的是来自ACF的子字段()
,该字段直接输出,不返回任何内容,因此无法将其结果传递给do\u shortcode()
您可以使用get\u sub\u field()
,捕获输出,然后将所有内容传递给do\u shortcode()
例如:
$linkDescription=get_sub_字段('link_description');
$renderedShortcode=do_shortcode(“[expand title=“Open”swaptitle=“Close”]$linkDescription[/expand]”);
echo$renderedShortcode;
如果在使用前需要检查短代码是否存在,则可以使用shortcode\u exists()
例如
如果(短码_存在('expand')){
echo do_短代码(“[expand title=“Open”swaptitle=“Close”]$linkDescription[/expand]”);
}
否则{
echo$linkDescription;
}
文件:
那太好了,谢谢你。如果我想先检查短代码(即插件)是否可用,如果不抛出一条消息说“短代码不可用”,我会怎么做?
<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 echo do_shortcode("[expand title=\"Open\" swaptitle=\"Close\"]".get_sub_field('link_description')."[/expand]") ?>
$output=do_shortcode("[expand title=\"Open\" swaptitle=\"Close\"]".get_sub_field('link_description')."[/expand]");