WordPress教程

WordPress 如何在文章段落后添加自定义内容

阿里云

如果想在 WordPress 文章段落后添加自定义内容,比如在文章第二段后添加广告,可以尝试用下面的代码:

section

  1.     // 在文章第二段之后插入内容
  2.     add_filter( 'the_content', 'prefix_insert_post_ads' );
  3.     function prefix_insert_post_ads( $content ) {
  4.     	$ad_code = '<div>添加的文字</div>';
  5.     	if ( is_single() && ! is_admin() ) {
  6.     		return prefix_insert_after_paragraph( $ad_code, 2, $content );
  7.     	}
  8.     return $content;
  9.     }
  10.  
  11.     // 主要函数
  12.     function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
  13.     	$closing_p = '</p>';
  14.     	$paragraphs = explode( $closing_p, $content );
  15.     	foreach ($paragraphs as $index => $paragraph) {
  16.     		if ( trim( $paragraph ) ) {
  17.     			$paragraphs[$index] .= $closing_p;
  18.     		}
  19.     		if ( $paragraph_id == $index + 1 ) {
  20.     			$paragraphs[$index] .= $insertion;
  21.     		}
  22.     	}
  23.     	return implode( '', $paragraphs );
  24.     }
也想出现在这里?联系我们
创客主机

section

分别在段落 2、4、6 后面添加不同的内容

  1.     add_filter( 'the_content', 'add_ads_to_content' );
  2.     function add_ads_to_content( $content ) {
  3.     	$ads = array(
  4.     		2 =>  'ad code 1', // paragraph_id => ad_code
  5.     		4 => 'ad code 2', // paragraph_id => ad_code
  6.     		6 => 'ad code 3' // paragraph_id => ad_code
  7.     	);
  8.  
  9.     	if ( is_single() && ! is_admin() ) {
  10.     		foreach ($ads as $paragraph_id => $ad_code) {
  11.     			$content = prefix_insert_after_paragraph( $ad_code, $paragraph_id, $content );
  12.     		}
  13.     	}
  14.     	return $content;
  15.     }
  16.  
  17.     function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
  18.     	$closing_p = '</p>';
  19.     	$paragraphs = explode( $closing_p, $content );
  20.     	foreach ($paragraphs as $index => $paragraph) {
  21.     		if ( trim( $paragraph ) ) {
  22.     			$paragraphs[$index] .= $closing_p;
  23.     		}
  24.     		if ( $paragraph_id == $index + 1 ) {
  25.     			$paragraphs[$index] .= $insertion;
  26.     		}
  27.     	}
  28.     	return implode( '', $paragraphs );
  29.     }

如果只显示在某个分类文章中添加,也可以这么写

  1.     add_filter( 'the_content', 'prefix_insert_post_ads' );
  2.     function prefix_insert_post_ads( $content ) {
  3.     	$ad_code = '<div>自定义文字</div>';
  4.     		if ( is_single() && ! is_admin() && in_category('29') ) {
  5.     		return prefix_insert_after_paragraph( $ad_code, $content );
  6.     	}
  7.     	return $content;
  8.     }
  9.  
  10.  
  11.     function prefix_insert_after_paragraph( $insertion, $content ) {
  12.     	$closing_p = '</p>';
  13.     	$paragraphs = explode( $closing_p, $content );
  14.     	$paragraph_id = round(count($paragraphs) / 2);
  15.     	foreach ($paragraphs as $index => $paragraph) {
  16.     		if ( trim( $paragraph ) ) {
  17.     			$paragraphs[$index] .= $closing_p;
  18.     		}
  19.     		if ( $paragraph_id == $index + 1 ) {
  20.     		$paragraphs[$index] .= $insertion;
  21.     		}
  22.     	}
  23.     	return implode( '', $paragraphs );
  24.     }
WordPress 如何在一定字数之后添加自定义内容 使用下面的代码,可以方便地在 WordPress 文章一定字数之后添加自定义内容。使用下面的代码,可以方便地在 […]
日期:2022/9/15 阅读:551阅读全文

WordPress 如何在文章段落后添加自定义内容

已有 169 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!