brendas Posted November 19, 2017 Share Posted November 19, 2017 Hello, This code add ads in the content. I want to change the output. I tried on it but I could not solve the problem. Thanks. add_filter( 'the_content', 'ads_paragraphs'); function ads_paragraphs( $content ) { $adsbeforeparagraph = array(1,3,5); global $post; $ad = 'ADS CODE'; $content_expl = explode("<p>", $content); for ($i = 0; $i <count($content_expl); $i++ ) { if (in_array($i, $adsbeforeparagraph)){ $content_expl[$i] = $ad . $content_expl[$i]; } } return implode("<p>", $content_expl); } input: <p>text text text</p> <p>text text text</p> <p>text text text</p> <p>text text text</p> <p>text text text</p> <p>text text text</p> output: <p>ADS CODEtext text text</p> <p>text text text</p> <p>ADS CODEtext text text</p> <p>text text text</p> <p>ADS CODEtext text text</p> <p>text text text</p> I want this output: ADS CODE<p>text text text</p> <p>text text text</p> ADS CODE<p>text text text</p> <p>text text text</p> ADS CODE<p>text text text</p> <p>text text text</p> Link to comment Share on other sites More sharing options...
Barand Posted November 19, 2017 Share Posted November 19, 2017 You could use array_walk $ad = 'ADS CODE'; $data = [ '<p>test test test</p>', '<p>test test test</p>', '<p>test test test</p>', '<p>test test test</p>', '<p>test test test</p>', '<p>test test test</p>' ]; array_walk ($data, 'add_prefix', $ad); function add_prefix (&$item, $key, $prefix) { if ($key%2 == 0) { $item = $prefix . $item; } } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.