Title: PHP array for specific pages only&#8230;
Last modified: August 31, 2016

---

# PHP array for specific pages only…

 *  [shawncbarry](https://wordpress.org/support/users/shawncbarry/)
 * (@shawncbarry)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/php-array-for-specific-pages-only/)
 * I’ll try to be brief, but comprehensive to not waste reader’s time.
 * I have an old site with a custom theme I created that is successfully displaying
   pages of certain categories and sub categories (the code snippet below). So, 
   in an attempt to do it differently (re-designing the theme), I used the same 
   code hoping to display a smaller number of the pages, but no longer sub-categories.
   for those who know PHP,it’s easier I imagine if you see the code (not just my
   bad description), but before I post the code…
 * The goal: show 5 random pages from a certain type of pages as an array, showing
   the page’s thumbnail and title, all of which are being grabbed from custom fields.
 * The problem: this code below grabs from ALL the pages on the site, not just the
   ones I’m trying to target.
 * Challenge: I’m a creative pro with HTML/CSS experience, but not much PHP experience.
   The original PHP code came from a tutorial (works great), but a tutorial that
   didn’t explain the alternate possibilities. Tried to troubleshoot this but am
   out of my range.
 * Let me know if this makes sense or is “too much to ask” in a post like this. 
   A bit stumped – help is appreciated.
 *     ```
       $categoriesCF = get_post_meta($post->ID, "categories", true);
       $allCategories = explode(",", $categoriesCF);
   
       		foreach ($allCategories as $category) {
       			$pieces = explode("|", $category);
       			$link = get_permalink($pieces[1]);
       			echo "<div>";
   
       query_posts("posts_per_page=5&post_type=page&post_parent=$pieces[1]&orderby=rand");
       while (have_posts()) : the_post();
       ?>
   
       <a href="<?php the_permalink(); ?>"><div>
       <img src="<?php echo get_post_meta($post->ID, 'modelImage', true); ?>" width="100%"/>
       <h2><?php echo get_post_meta($post->ID, 'name', true); ?></h2>
       </div></a>
   
       <?php endwhile; wp_reset_query();
   
       echo "</div>";	
   
       };
       ```
   

The topic ‘PHP array for specific pages only…’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [categories pages](https://wordpress.org/support/topic-tag/categories-pages/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 0 replies
 * 1 participant
 * Last reply from: [shawncbarry](https://wordpress.org/support/users/shawncbarry/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/php-array-for-specific-pages-only/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
