Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nowadays, it is so easy to get started with a website and all thanks to the Content Management Systems (CMS) like WordPress, Drupal, Joomla, and many others like them. However, if we talk about the reliability and ratings, WordPress definitely rules…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
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…

863 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

26 Experts available now in Live!

Get 1:1 Help Now