SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    putting related_posts in the_content

    Hi there,

    I am using the code below to insert content in the middle of a single article on my blog. The website is designed in PHP and the function that calls the related articles is
    Code PHP:
    <?php related_posts() ?>

    This is the code that I am using to insert it in the middle of a post.


    Code PHP:
    function inject_ad_text_after_n_chars($content) {
      // only do this if post is longer than 1000 characters
      $enable_length = 3000;
      // insert after the first </p> after 500 characters
      $after_character = 1500;
      if (is_single() && strlen($content) > $enable_length) {
        $before_content = substr($content, 0, $after_character);
        $after_content = substr($content, $after_character);
        $after_content = explode('</p>', $after_content);
        $text = '[B]Related Posts Function Goes Here[/B] ';
        array_splice($after_content, 1, 0, $text);
        $after_content = implode('</p>', $after_content);
        return $before_content . $after_content;
      }
      else {
        return $content;
      }
    }
    add_filter('the_content', 'inject_ad_text_after_n_chars');

    I just need somebody to tell me how to translate my function <?php related_posts() ?> and put it in the line I have indicated above where it says "Related Posts Function Goes Here."

    Any help would be appreciated.

    Thanks.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    To do that, you want an output buffer.

    The related_posts() function, based on your usage, would output to the browser instead of returning the posts as a string, so you would use the following to get the output in a variable:
    PHP Code:
    ob_start();
    related_posts();
    $RelatedPosts ob_get_clean(); 
    Soooo....
    PHP Code:
    function inject_ad_text_after_n_chars($content) {
      
    // only do this if post is longer than 1000 characters
      
    $enable_length 3000;
      
    // insert after the first </p> after 500 characters
      
    $after_character 1500;
      if (
    is_single() && strlen($content) > $enable_length) {
        
    $before_content substr($content0$after_character);
        
    $after_content substr($content$after_character);
        
    $after_content explode('</p>'$after_content);
        
    ob_start();
        
    related_posts();
        
    $text ob_get_clean();
        
    array_splice($after_content10$text);
        
    $after_content implode('</p>'$after_content);
        return 
    $before_content $after_content;
      }
      else {
        return 
    $content;
      }
    }
    add_filter('the_content''inject_ad_text_after_n_chars'); 
    However, the related_posts() function might require some information (i.e. the post you want to find related posts for) before getting the desired results.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! It works perfectly exactly as you have written it - no additional information needed. The information used to determine which posts to find related items for is already built into the related_posts() function itself in the form of a plugin

    You have been a great help!

    Thank you once more

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would you be able to help me with 1 other thing related to this...

    Normally, the related posts are shown at the end of each article. Using this modification, I've been able to move it to the middle of the post but only if the post is more than 3000 characters in length. Any less than that and I want it to stay at the bottom of the post as normal.

    So I currently have this new code in place but in order to make sure the page still has related articles on it when posts are less than 3k characters, I've had to leave the related_posts() function at the bottom of the page.

    How do I add an if statement to the bottom function to check if the new code has been called and only display at the bottom if it hasn't.

    I hope that makes sense.

    This is the code I use for the related posts at the bottom of the page:

    Code PHP:
    <p><div class="related"> <?php related_posts() ?> </div></p>

    So just to clarify - I want to add an if statement to the above code so that it only displays if our new function hasn't already displayed the related articles in the middle of the page.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just put that in the filter function itself, i.e:

    PHP Code:
    function inject_ad_text_after_n_chars($content) {
      
    // only do this if post is longer than 1000 characters
      
    $enable_length 3000;
      
    // insert after the first </p> after 500 characters
      
    $after_character 1500;
      
    ob_start();
      
    related_posts();
      
    $text ob_get_clean();
      if (
    is_single() && strlen($content) > $enable_length) {
        
    $before_content substr($content0$after_character);
        
    $after_content substr($content$after_character);
        
    $after_content explode('</p>'$after_content);
        
    array_splice($after_content10$text);
        
    $after_content implode('</p>'$after_content);
        return 
    $before_content $after_content;
      }
      else {
        return 
    $content $text;
      }
    }
    add_filter('the_content''inject_ad_text_after_n_chars'); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect

    One last question. I'm sorry, I hope I'm not getting on your nerves...

    I also have a plugin which links to a forum for people to discuss the article, it has a link underneath the article - and I was hoping to get the "related articles" underneath this link if it isn't already in the middle of the article...

    With your kind help, I've managed to get it to the bottom of the post content - but is there a way I can push it even further and have it after the discuss link.

    This is how the page is coded:

    Code PHP:
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    <div class="discuss"><?php if (function_exists("wp2bb")) wp2bb(); ?> </div> </div>

    Our function is contained within the "the_content" function above. It is all built in... so it's putting everything we've done above the Discuss class.

    Would I need to put some "if" statements on this page to make this work?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •