Displaying Custom Data within Post Content

Being able to add dynamic custom content within the Post content could a great way to promote your posts or display advertisement. Recently I came across a question from one of our fellow user on Facebook Help group who was asking for a plugin to display related posts within the post content. Even though I replied to his question and provided the code with possible solution, I still feel like I should write a post with similar concept.

So, today in this post I will show you how to display custom content within the post content. In this case I will work with the post paragraphs and place a custom div element where you can also place your custom content. This snippet could be really useful for those who wants to place advertisement within the content. No, I am not talking about placing ad at the very beginning of the content rather it would be visible after a specific number of paragraph. Definitely you would have the option to change that number.

You can simply copy and paste this snippet on your theme’s functions.php page and update it. Yes, that’s all you need to do. You don’t need to touch your single.php page in anyway or form as this function gets hooked with another action and it will affect the_content function. So, let’s take a look at the snippet.

<?php
function display_custom_post_content($content) {
global $post;
   if (is_single()) {
   $after = 1; // number of paragraph
   $content = explode ("</p>", $content);
   $data = '';
      for ($i = 0; $i < count ($content); $i++) {
      if ($i == $after) {
         $data .= 'YOUR CUSTOM CONTENT'; }
         $data .= $content[$i] . "</p>";}
         return $data; 
      }  else { 
         return $content;}}	
add_filter('the_content','display_custom_post_content');
?>

I tried to make this function as simple as I could have and it seems like its working. This snippet has been tested on my computer and also being used on this site.

Explanation:

In this snippet, I created a function and called the global $post variable. With an if condition I checked if it is a single post page using is_single function. If it is true then it would perform rest of the function.

One important variable here is $after, this is where you can decide where exactly you want your custom content should appear. Based on the numerical value that you add here, your custom content would appear right after that number of paragraph. Even though by default I added 1 you can change it with any number you want. Now, change your text where it says “YOUR CUSTOM CONTENT”. You can place any kind of JavaScript or Advertising code here. For styling purpose you can place your content withing a div or span element and style them with CSS.

Note: If you want to use this snippet for Custom Post Type, simply replace is_single() function with is_singular(‘cpt’), rest of the function should remain as it is.

Feel free to try it out and let me know how did it go. Any question or query on this regard are highly welcome.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.