Solved

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

Posted on 2013-06-30
3
513 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Read about how to approach blogging and about ways to do it right. Stand out from the crowd and let your knowledge be consumed by a large audience. This article aims to explain how your blog should look like,  the most important things to do while b…
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 purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now