Wednesday, August 22, 2012

Wordpress: How to retrieve multiple page content?

Here is a quick snippet to retrieve content from multiple pages and display on any page, post or template. You  can even try creating a short-code for this functionality and could pass page id's to in the argument.

Below, is the screenshot for what I achieved from this code.



<?php
  /*
  Pass the page id's in an array.
  */
  $home_pages = array('11', '13', '16');
  $output =''; // Output variable

foreach($home_pages as $key => $val) {
      $output .= '<div class="box">';
      $page_id = $val; 
      $page_data = get_page( $page_id ); 
      $output .= get_the_post_thumbnail($page_id);
      $page_link = $page_data->guid; //This for SEF permalinks
      $page_link = explode('/?', $page_link);
      $page_link = $page_link[0];
      $page_link = $page_link . '/' . $page_data->post_name;
      $output .= '<h3>'. $page_data->post_title .'</h3>';
      $content = apply_filters('the_content', $page_data->post_content); 
      $content = substr($content, 0 , 100);
      $output .= $content . '...';
      $output .= '<br /><a href="'.$page_link.'" title="Read More">Read More...</a>';
      $output .= '</div>';
 }
 
 echo $output;
?>

No comments:

Post a Comment