1. 当前位置:
  2. 首页
  3. »
  4. 随笔
  5. »
  6. wordpress the_content内容替换

wordpress the_content内容替换

零分 3,311

在wordpress the_content是输出文章内容,是带有标签的,比较明显的就是P标签了。 the_content 使用直接<?php the_content();?>,还有一个返回值  get_the_content,输出文章内容用 <?php echo get_the_content();?>

今天在做弹出图片效果的时候,auto highslide 插件没效果,用fancybox也没有效果,查看了下源码,fancybox需要添加几个参数,要对文章内容进行批量添加。直接用 <?php echo get_the_content();?> 发现全部都没有P标签。

用 <?php the_content();?>要在函数模版写一个修改的函数

function my_the_content_filter($content) {
    $contents = preg_replace('~<img (.*?) src=(.*?)>~s','<center><img $1 src="'.get_bloginfo("template_url").'/img/loading.gif" data-original=$2></center>',$content);
    $pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>(.*?)<\/a>/i";
    $replacement = '<a$1href=$2$3.$4$5 class="fancybox" data-fancybox-group="gallery" title="'.get_the_title().'" $6>$7</a>';
    $content = preg_replace($pattern, $replacement, $contents);
      return $content;
     }
add_filter( 'the_content', 'my_the_content_filter' );

这边顺便对文章图片进行了一下居中,修改用正则。

头像
支持作者
联系微信二维码
0%