wordpress收费下载资源主题
wordpress付费下载资源插件

WordPress 常用函数 / has_shortcode

简介

检查内容中是否含有指定的 Shortcode。

用法

<?php if ( has_shortcode( $content, 'gallery' ) ) { } ?>

参数

$content
(string) (required) 要检测的内容
Default: None

$tag
(string) (required) 指定的 Shortcode
Default: None

返回值

(Bool)
指定的 Shortcode 找的返回 true,否则 false。

实例

if( has_shortcode( $content, 'gallery' ) ) {
// 内容中有gallery Shortcode,将返回 true。
}

在内容中含有指定的 Shotcode 的时候才载入相应的脚本。

function custom_shortcode_scripts() {
global $post;
if( has_shortcode( $post->post_content, 'custom-shortcode') ) {
wp_enqueue_script( 'custom-script');
}
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_scripts');

修改记录

Since 3.6.0

源文件

wp-includes/shortcodes.php.

0 个评论

定制开发
本站承接WordPress等系统建站仿站、二次开发、主题插件定制等开发服务
在线咨询
  • 请直接说明需求,勿问在否
    QQ:1-247-246-247

  • QQ一群:104228692(满)
  • QQ二群:64786792
在线咨询
本站承接WordPress建站仿站、二次开发、主题插件定制等PHP开发服务!

了解详情