我正在使用do_shortcode
函数在我的模板中添加快捷码。但我想在显示之前检查一下这个短代码是否存在。
我是说像这样
If (shortcode_gallery_exists) {
echo do_shortcode(\'[gallery]\');
}
有人能帮我吗?谢谢我正在使用do_shortcode
函数在我的模板中添加快捷码。但我想在显示之前检查一下这个短代码是否存在。
我是说像这样
If (shortcode_gallery_exists) {
echo do_shortcode(\'[gallery]\');
}
有人能帮我吗?谢谢#23572 介绍shortcode_exists() 转化为3.6。
我认为您可以使用以下代码来执行此操作:
$content = get_the_content();
//write the begining of the shortcode
$shortcode = \'[gallery\';
$check = strpos($content,$shortcode);
if($check=== false) {
//Code to execute if there isn\'t the shortcode
} else {
//Code to execute if the shortcode is present
}
(警告:未测试)您可以创建自己的函数,
// check the current post for the existence of a short code
function has_shortcode( $shortcode = NULL ) {
$post_to_check = get_post( get_the_ID() );
// false because we have to search through the post content first
$found = false;
// if no short code was provided, return false
if ( ! $shortcode ) {
return $found;
}
// check the post content for the short code
if ( stripos( $post_to_check->post_content, \'[\' . $shortcode) !== FALSE ) {
// we have found the short code
$found = TRUE;
}
// return our final results
return $found;
}
在模板中编写一个条件,如,if(has_shortcode(\'[gallery]\')) {
// perform actions here
}
来自此的想法NetTuts link在网上某处找到此文件并使用了一次或两次
//first we check for shortcode in the content
$tempContent = get_the_content();
$tempCheck = \'[gallery\';
$tempVerify = strpos($tempContent,$tempCheck);
if($tempVerify === false) {
//Your Shortcode not found do nothing ? you choose
} else {
echo do_shortcode(\'[gallery]\');
}
。(我知道[画廊缺了]…就这样吧)
This should be used inside the loop..<希望这有帮助,萨吉夫
WordPress允许您检查是否存在短代码。
要检查它,您可以使用shortcode_exists()
作用如果短代码存在,则返回true。
<?php if ( shortcode_exists( $tag ) ) { } ?>
在哪里$tag
要检查的短代码的名称。<?php
if ( shortcode_exists( \'latest_post\' ) ) {
// The short code exists.
}
?>
我知道这是个老问题,但有些人可能仍然需要这个,特别是因为shortcode_exists()
可能不适用于某些插件短代码:
$shortcode_result = do_shortcode(\'[your_shortcode]\');
if ($shortcode_result != [your_shortcode]) &&
!empty($shortcode_result))] {
// do what you need
} else {
// this is not valide shortcode
}
WP现在有一个内置功能:
<?php
if ( shortcode_exists( \'gallery\' ) ): ?>
// The short code exists.
<?php else: ?>
// It DOESN\'T EXIST
<?php endif;?>
https://developer.wordpress.org/reference/functions/shortcode_exists/
我继承了一个Wordpress网站,我的公司在我开始在这里工作之前就建立了这个网站。我会去问建造它的人这个问题,但他现在已经离开了公司。我的查询与wordpress类别的自定义模板相关。我知道您可以创建一个名为“类别-”的模板文件。当访问该类别时,它将使用该自定义模板。在本例中,该文件称为“category limousin magazine”。php\'这很好——我的问题涉及到这个类别的子类别,即在《利木赞》杂志下,我有2011、2010、2009、2008、2007和2006个类别。不知何故,在建立网