欢迎来到路由器窝!

WordPress——判断文章中是否有图片

浏览次数:

作者: 路由器窝

信息来源:

更新日期: 2020-06-12 08:43

文章简介

首先在functions.php添加以下代码: /***判断文章中是否有图片*/function is_has_image(){ if ( is_single () || is_page()) { global $post; if( has_post_t

  • 正文开始
  • 相关文章
首先在functions.php添加以下代码:
/**
*判断文章中是否有图片
*/
function is_has_image(){
   if ( is_single () || is_page()) {
    global $post;
    if( has_post_thumbnail() ) return true;
    $content = $post->post_content;
 
    preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
 
    if(!empty($strResult[1])) return true;
    return false;
}
}

在需要的地方进行调用:
<?php if( is_has_image() ) :?>
   ......//功能代码
<?php endif;?>

转载请注明:Wordpress» WordPress——判断文章中是否有图片

标签:
  • contact-form-7历史版本WP SMTP配合使用
    809阅读
    https://github.com/rocklobster-in/contact-form-7/tags
  • WordPress 无插件 纯代码实现分页导航
    198阅读
    第一步:将以下代码放到自己使用的wordpress主题的模板函数文件functions.php中。 function kriesi_pagination($query_string){global $posts_per_page, $paged;$my_query = new WP_Query($query_strin...
  • 提交留言-安装超级留言插件
    226阅读
    form action=/wp-comments-post.php class=form-horizontal method=post name=myform id=commentform style= width: 90%; margin: 40px auto 0; input type=hidden name=re...
  • Wordpress当前位置调用
    218阅读
    进入自己做网站的后台,找到主题下面的编辑按钮; 将以下的代码复制到模板函数functions.php里; //面包屑function wheatv_breadcrumbs() { $delimiter = ; $name = 首页; if ( !is_home() ||!is_front_page() || is_pa...
  • 菜单注册
    625阅读
    function wpb_custom_new_menu() { register_nav_menu('custom-menu',__( 'Custom Menu' ));}add_action( 'init', 'wpb_custom_new_menu'...
  • 添加基本编辑字段公共字段
    728阅读
    //添加自定义字段function my_profile( $contactmethods ) {$contactmethods['tel'] = 'tel'; //增加$contactmethods['fax'] = 'fax';$contactmeth...
  • 后台菜单a标签添加样式
    703阅读
    function xintheme_menu_link_atts( $atts, $item, $args ) { $atts['class'] = 'menu-link'; $atts['data-link-alt'] = $item->title; ret...
  • WordPress True / False功能参数
    929阅读
    https://www.advancedcustomfields.com/resources/true-false/ True / False 字段返回布尔值true或false。请注意,保存到数据库中的实际值是 1 或 0 的整数。<?phpif( get_field('color') ) { ...
  • 自定义面包屑
    940阅读
    //面包屑function get_breadcrumbs(){global $wp_query;if ( !is_home() ){// Start the ULecho ' ';// Add the Home linkecho '<dd><a href="'...
  • 列表描述改编辑器
    757阅读
    ///描述改编辑器add_action("category_edit_form_fields", 'edit_form_fields_example', 10, 2);add_action("category_add_form_fields", 'add_form_fields_...
上一页:提交留言-安装超级留言插件
下一页:WordPress获取当前页面URL地址的方法
最近更新作品
acf判断
更新时间:2024-04-17

892人已经看过了!

指定栏目ID另类调用配合acf
更新时间:2024-04-16

857人已经看过了!

word press 自定义文章类型制作留言功能
更新时间:2024-03-10

985人已经看过了!

php if  ifelse 循环
更新时间:2024-03-09

576人已经看过了!

栏目模板选择插件栏目模板
更新时间:2024-03-08

738人已经看过了!

contact-form-7历史版本WP SMTP配合使用
更新时间:2024-03-08

809人已经看过了!

WordPress True / False功能参数
更新时间:2024-03-08

929人已经看过了!

wordpress调用指定分类置顶文章
更新时间:2024-03-07

519人已经看过了!

WordPress父分类调用子分类名称和文章列表
更新时间:2024-03-07

520人已经看过了!