WordPress.org

Ready to get started?Download WordPress

Forums

Determine automatically the middle of the post, from word count (9 posts)

  1. tr3ndy
    Member
    Posted 3 years ago #

    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?

  2. popper
    Member
    Posted 3 years ago #

    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.

  3. tr3ndy
    Member
    Posted 3 years ago #

    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

  4. popper
    Member
    Posted 3 years ago #

    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++;
     }
    ?>
  5. tr3ndy
    Member
    Posted 3 years ago #

    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?

  6. keesiemeijer
    moderator
    Posted 3 years ago #

    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;
    	?>
  7. tr3ndy
    Member
    Posted 3 years ago #

    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.

  8. Joe Greenwood
    Member
    Posted 3 years ago #

    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/

  9. Joe Greenwood
    Member
    Posted 3 years ago #

    <?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>";
        } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic