• Assuming, that a post is at least 150 word long, I’ll try to insert automatically in the middle, some code or banner. I haven’t find nothing of conclusive in searching with Google.

    Anyone has some ideas?

Viewing 8 replies - 1 through 8 (of 8 total)
  • popper

    (@julialasarte)

    Something like this should work:

    <?php
    $content = apply_filters('the_content', $post->post_content);  //get the post content store in $content
    // find the middle
    $full_size = STRLEN($content);
    $halfway_mark = ($full_size / 2);
    
    // clip off the first half
    $firsthalf = SUBSTR($content, 0, $halfway_mark);
    
    // find the last '<br>' in the first half
    $end_mark = STRRPOS($firsthalf, '<br>');
    // if that doesn't do it, find the first </p>
    if(!$end_mark) {$end_mark = STRRPOS($firsthalf, '</p>');}
    
    // add in the ADD at the position found above
    $content = SUBSTR($content, 0, $end_mark) . "<p> YOUR ADD CODE HERE <p>" . SUBSTR($content, $end_mark); 
    
    echo $content;
    ?>

    you could put that in your single.php instead of the_content(). It’s not very pretty, but it does the job. Alternatively, you could turn that into a little plugin, so your template code remais clean.

    Thread Starter tr3ndy

    (@tr3ndy)

    Mmm something wrong,
    I’ve changed my single.php file,but the YOUR ADD CODE HERE message, appears at the beginning of the post, under the main title

    popper

    (@julialasarte)

    Mhm, is it possible that there are neither </p> nor </br< in the first half of the post. You could just cut at the middle, but it’ll probably cut words. Another solution is to put the code after a given number of paragraphs, say 2 or 3, and in average the ad will be close to the middle.

    <?php
      $content = apply_filters('the_content', $post->post_content);  //get the post content store in $content
      $save = explode("</p>", $content);  //Separate the content into <p> blocks
      $tcount=0; //this is count for number of <p> blocks
      $adon=0;  //this is a variable so you don't show ads more than once.
      foreach($save as $item) {
        echo $item;  //print the <p> block
        echo "</p>";
        if(preg_match('/<p> /',$item)==0 && $tcount>=1 && $adon==0) {
                                    $adon=1;
    ?>
    Replace this with Your adsense code!!!
    <?php
        }
     if(preg_match('/<p> /',$item)==0 && $tcount>=4 && $adon==1) {
                                    $adon=2;
    ?>
    Replace this with Your adsense code if you want additional adsense blocks for super long blog posts or leave it empty if you don't want one.
    <?php
       }
       $tcount++;
     }
    ?>

    Thread Starter tr3ndy

    (@tr3ndy)

    Sure in the post, there wasn’t no tags like </p> or
    so cut at middle results impossible. Your second solution is really interesting, but I’ve a multiathour blog and if they forgive to add a paragraph, nothing happens, or better the code was inserted at the end of the post.

    The unique solution is the cut on the middle, and add the banner after the first, blank space (spacebar), instead to cut words? Is it possibile in your opinion?

    Moderator keesiemeijer

    (@keesiemeijer)

    You can try it with this also. It breaks your content in two and doesn’t cut words. Must be used inside the loop:

    <?php
    	$content = apply_filters('the_content', $post->post_content);
    	$content = explode(' ', $content);
    	$halfway_mark = ceil(count($content) / 2);
    	$first_half_content = implode(' ', array_slice($content, 0, $halfway_mark));
    	$second_half_content = implode(' ', array_slice($content, $halfway_mark));
    	echo $first_half_content.'...';
            echo 'Your adsense code';
            echo $second_half_content;
    	?>

    Thread Starter tr3ndy

    (@tr3ndy)

    Hi keesiemeijer,
    yes this time works like a charm.

    I try some more tests now.
    Thank you all for the help.

    If all goes ok, I plan to do a simple plugin to share with all.

    Regards.

    keesiemeijer, thank you … any idea how to count paragraphs of a post so that I could insert ads etc after x paragraphs? instead of middle of post

    i find this but my page doesn’t load with the code given: http://www.wpbeginner.com/wp-tutorials/how-to-insert-ads-within-your-post-content-in-wordpress/

    <?php
        $paragraphAfter= 1; //shows image after paragraph 1
        $content = apply_filters('the_content', get_the_content());
        $content = explode("</p>", $content);
        for ($i = 0; $i <count($content); $i++) {
        if ($i == $paragraphAfter) { ?>
    <div>INSERT HTML STUFF HERE</div>
        <?php
        }
        echo $content[$i] . "</p>";
        } ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Determine automatically the middle of the post, from word count’ is closed to new replies.