当前位置: 首页 » 笔记 » wordpress the_content内容替换正文

wordpress the_content内容替换

  • 零分
  • 3,101

在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' );

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

声明:博客中如无特殊说明或标注的文章均为博客原创文章,部分文章来源互联网,如有侵犯您的版权,或者对零分博客发布的文章有异议,请来信告知删除、修改,如您有好的建议或者意见也可以来信,谢谢!

请TA喝杯水^_^ 如本文“对您有用”,欢迎随意打赏,让我们坚持创作! 请TA喝水