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

wordpress the_content内容替换

零分笔记2,7332023-01-30 23:52

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

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

END
零分站龄17年资深站长
一个喜欢折腾,却又折腾不出像样东西的,不会PHP的PHP程序员!
2517
文章
13
分类
3818
标签
3
友链
onlinelovesky317355746vipsever@vip.qq.com