昊天SEO

WordPress在body中随机插入一张图片

 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(
 "%s",get_bloginfo( 'stylesheet_directory' ),$content
 );

// Returns the content.
 return $content;
 }

这是在字段的前面加的图片

在字段中间怎么加图片呢?

 

//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 );
}

本文地址官网    新乡网站建设,seo

分享到:更多 ()

相关推荐

a