1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
add_filter( 'the_content', 'my_the_content_filter', 20 ); /** * Add a icon to the beginning of every post page. * * @uses is_single() */ function my_the_content_filter( $content ) { if ( is_single() ) // Add image to the beginning of each page $src = rand(1,293)."."."jpg"; $content = sprintf( "<img data-original="%s/d/$src" />%s",get_bloginfo( 'stylesheet_directory' ),$content ); // Returns the content. return $content; } |
这是在字段的前面加的图片
在字段中间怎么加图片呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
//Insert ads after second paragraph of single post content. add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { $ad_code = '就是这里插入一张图片'; $src = rand(1,293)."."."jpg"; $ad_code = sprintf( "<img data-original=%s/d/$src >%s",get_bloginfo( 'stylesheet_directory' ),$content ); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $ad_code, 6, $content ); } 0 return $content; } // Parent Function that makes the magic happen function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</p>'; $paragraphs = explode( $closing_p, $content ); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } if ( $paragraph_id == $index + 1 ) { $paragraphs[$index] .= $insertion; $insertion.=$ad_code ; } } return implode( '', $paragraphs ); } |

