Solved

How to query pages and return title, thumbnails, excerpts and custom field data

Posted on 2013-06-30
3
515 Views
Last Modified: 2013-07-03
Hi,
I have set up child pages with each having its own category name.  I have a main page that I want to pull in the thumbnail (featured image), title, excerpt, and custom field data (not sure what this will be yet) for each of these child pages. Please note that these are not posts but pages.

I have created a custom page template for the main page but need help on how to add in the query for the child pages data.

Thanks
dukiejc
0
Comment
Question by:dukiejc99
  • 2
3 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39290554
That's extremely vague. Remember, we have no idea of what your site / code currently looks like. The most information I can infer is that you're working with a WP site.

That said, I would really recommend just going into the database using a GUI tool like HeidiSQL and viewing the tables and data, and then test out your queries there.
0
 

Author Comment

by:dukiejc99
ID: 39292017
Hi,
Yes, I'm working with a WP site.  Sorry, I'll try to explain it in more details.  I'm afraid i'm not familiar with working with database as you suggested.

I've created a child theme and what i'm trying to do is to have a main page featuring a list of contributors.  I went and created a child page for each of the contributor.  I've also enabled categories and excerpts on the pages hoping i'll be able to use them somehow.  

On my main page, i'm trying to figure out how to be able to pull in from these child pages, the title, thumbnail (added as featured image), excerpts and possibly data from a custom field.  I'm not sure if this is even possible to do it like pulling in posts.  

Here is what I have so far below but it doesn't pull in what I need but just the full content of the page.

<?php get_header(); ?>

<div id="content-area" class="clearfix<?php if ( $fullwidth ) echo ' fullwidth'; ?>">
      <div id="left-area">
            <?php get_template_part('includes/breadcrumbs', 'page'); ?>

            <?php
      $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );

      foreach( $mypages as $page ) {            
            $content = $page->post_content;
            if ( ! $content ) // Check for empty page
                  continue;

            $content = apply_filters( 'the_content', $content );
      ?>
            <h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
      
            <div class="entry"><?php echo $content; ?></div>
      <?php
      }      
            ?>

      </div> <!-- end #left-area -->

      <?php if ( ! $fullwidth ) get_sidebar(); ?>
</div>       <!-- end #content-area -->

<?php get_footer(); ?>
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39293400
First, if you're not familiar with objects in PHP, you might want to read up on them a bit. Wordpress's API works with objects a lot.

Next, make sure you have the WP Function Reference handy - it will save your skin when you're building out WP customizations:
https://codex.wordpress.org/Function_Reference

Next, you should understand the difference between pages and posts. Pages are more "static" content while "posts" are more dynamic. For example, my blog has an "About" page that stays at a specific URL and is rarely updated and then the blog has posts which are technical write-ups that I add all the time.

Finally, the functions you'll probably want to use are:
https://codex.wordpress.org/Function_Reference/get_pages
https://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

You're already using get_pages() but from what you've written, it sounds like you don't realize all of the other information that is coming back and is already available (e.g. each object in $mypages has a "post_excerpt" containing a preview of the content. If you go to the get_pages link above and scroll down to the Return section, you can see all of the data that is available to you when you work with a page object.

The thumbnails HTML can be retrieved on a per-page basis using this code:
echo get_the_post_thumbnail($page->ID, 'thumbnail');
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
SEO can be a real minefield to navigate, but there are three simple ways to up your SEO game just be re-assessing your content output.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to count occurrences of each item in an array.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question