Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Can's get page to display on a single page.

I've been trying to get pull all the pages to display on a single page, e.g front-page.php.

<?php 

    query_posts('post_type=page&order=ASC'); 

    // OR

    $args = array(
        'post_type' =>'page',
        'order' => 'ASC'
        );

    $the_query = new WP_Query( $args );

?>

all pages seem to be displaying onto the page ok, but when creating a template for a certain page nothing is showing up.

13 Answers

Stephen O'Connor
Stephen O'Connor
22,291 Points

What template are you using for your pages? A standard WordPress loop should bring in page content if you are using page.php.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <!-- Gets the title and content -->
    <h1><?php the_title(); ?></h1>
    <?php the_content(); ?>

<?php endwhile; else: ?>

    <!-- Displayed if no posts or pages are available -->
    <p>There are no posts or pages here!</p>

<?php endif; ?>

here is my codes:

$args = array(
               // 'year' => $year,
              // 'w'    => $week    
              'post_type' => 'project'              
            );

            $the_query = new WP_Query( $args );             


?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <?php  get_template_part( 'content', 'page' ); ?>


<?php endwhile; // end of the loop. ?>

<?php /**

  • Template Name: Menu */ ?>

<?php <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php $args = array(
'post_type' => 'menu'
); ?>

<?php the_field('menu_descriptions'); ?>

</article>

Stephen O'Connor
Stephen O'Connor
22,291 Points

What are you trying to display on the page, just the content/title from each page or something else?

I'm trying to display 'custom fields' from the custom post.

Stephen O'Connor
Stephen O'Connor
22,291 Points

Are you using WordPress custom fields or a plugin like Advanced Custom Fields?

Yes I am.

Stephen O'Connor
Stephen O'Connor
22,291 Points

Which one? WordPress' own custom fields or the ACF plugin?

sorry, it's ACF plugin that I'm using.

Stephen O'Connor
Stephen O'Connor
22,291 Points

OK. So you can just use the following to display the custom field(s) in whichever order you like within the standard WordPress loop - if your custom fields are assigned to the page:

<?php the_field('your_custom_field_name'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <!-- Custom field -->
    <?php the_field( 'your_custom_field_name' ); ?>

<?php endwhile; else: ?>

    <!-- Displayed if no posts or pages are available -->
    <p>There are no posts or pages here!</p>

<?php endif; ?>

Or you can use WP_Query if you are using a custom post type as well as custom fields:

<?php

    $args = array(
        'post_type' => 'your_custom_post_type'
    );

    $the_query = new WP_Query( $args );
?>

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <!-- Custom field -->
    <?php the_field( 'your_custom_field_name' ); ?>

<?php endwhile; else: ?>

    <!-- Displayed if no posts or pages are available -->
    <p>There are no posts or pages here!</p>

<?php endif; ?>

Also check out the ACF Documentation for more info.

Hi guys apologize for late reply.

I've managed to get all other pages to display on one page but, when I tried to have a custom page e.g " page-about.php " - nothing is showing on up that page? Obviously when displaying" page-about.php " on it own, everything displaying correctly on the page. I've been scratching my head for days now and trying to figure this out.

anyone could help?