Return to Snippet

Revision: 28671
at July 12, 2010 14:17 by scitrenbaumgmailcom


Initial Code
Add following code in your functions.php file inside your theme folder.

<?php
if(!function_exists('getPageContent'))
	{
		function getPageContent($pageId,$max_char)
		{
			if(!is_numeric($pageId))
			{
				return;
			}
			global $wpdb;
			$nsquery = 'SELECT DISTINCT * FROM ' . $wpdb->posts .
			' WHERE ' . $wpdb->posts . '.ID=' . $pageId;
			$post_data = $wpdb->get_results($nsquery);
			if(!empty($post_data))
			{
				foreach($post_data as $post)
				{
					$text_out=nl2br($post->post_content);
					$text_out=str_replace(']]>', ']]&gt;', $text_out);
					$text_out = strip_tags($text_out);
					return substr($text_out,0,$max_char);

				}
			}
		}
}
?>


And to display text you have to call the function like

<?php echo getPageContent(11,150); //First parameter is PAGE ID and second is number of words displayed. ?>

Initial URL


Initial Description


Initial Title
Wordpress - Get page content by Page ID

Initial Tags


Initial Language
PHP