Solved

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

Posted on 2013-06-30
3
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

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 35

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
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 reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

691 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