PHP script to include page and post using WordPress excerpt

Developers are developing the site and it doesn’t matter what technology and platform they are using. but while developing the site sometimes they required to include one page into another page. and you are a developer then must have an idea about it but if you are not a developer then let me give you a short description about include.
Include the name suggest you that it includes something. So in a technical way using include functionality you can show other page content into another page.Suppose I have one page i.e about ultimate blogger and I want to show this content dynamically into another page then I will use include functionality to show it.

In the PHP we are using the following function to include the page.


WordPress is a CMS and many developers has no ideas how to include one page or post into other page using code. In generally we are using readymade plugins and its shortcode to include the pages and post. But sometimes included page will not show the updated content . So, Today I will explain to you how to include page and post into an, Today I will explain to you how to include page and post into the other page using WordPress excerpt.Use the following code to do the same.

function intro_text($length){
  $postid = 1;//This is page id or post id
  $content_post = get_post($postid);
  $content = $content_post->post_content;
  $content = apply_filters('the_content', $content);
  $content = str_replace(']]>', ']]>', $content);
  $length= 150;
  if (strlen($content) > $length){
    $content = substr($content,0,strpos($content,' ',$length)) . ' ... <a href=""' . get_permalink(2) . '"">Read more.</a>';
  return apply_filters('the_excerpt',$content);

echo intro_text($length); 

In the above code replace the $post id with the current page or post id. and $length will be dependent on you choice.

You just need to put above function in the function.php file or anywhere and you need to use following code where you want to show included content.

echo intro_text($length)

Make sure that you are using this function and code into PHP files and not in the WordPress default editor.

I hope this will help you a lot while developing the site. Let me know if you any queries related to this.


Leave a Reply

Your email address will not be published. Required fields are marked *

17 + eleven =

You May Like Also

%d bloggers like this: