0

On my home page I'd like to display the title and content from a few pages (about page, contact page, etc.). It looks like the template tag get_post can be used, but I'm not savvy enough with PHP to make it so.

I found the code snippet below and it works.

<?php $id = 17; $post = get_page($id); $content = apply_filters('the_content', $post->post_content); $title = $post->post_title; echo '<h3>'; echo $title; echo '</h3>'; echo $content; ?>

0

You can use the below query to get content from specific pages by id or page slug

// WP_Query arguments
$args = array(
    'page_id'                => '12,14,78,89',//replace the page ids
    //'pagename'               => 'aboutus, contact', //or use page slugs
    'post_type'              => array( 'page' ),
    'post_status'            => array( 'publish' ),
    'posts_per_page'         => '10',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<h3>';
        echo $the_title; 
        echo '</h3>';
        echo $the_content;
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();
  • I tried this but it would only output one of the pages. I tried putting the page_id items in an array, but that didn't help either. – Nick J. Oct 23 '18 at 20:07
  • @NickJ. Please post your code – Latheesh V M Villa Oct 23 '18 at 20:28
  • I copied the code that you provided and changed the page IDs. It only outputted one of the pages. I provided two other solutions that did work. – Nick J. Oct 23 '18 at 22:54
0

Based on Latheesh's guidance, I came up with a working solution. I did have to create a parent page and place the pages I wanted to appear as children of parent 46:

<?php
    $args = array(
        'post_parent'            => '46',
        'post_type'              => 'page',
        'order'                  => 'ASC'
    );
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
        echo '<section class="page-content">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<h2>' . get_the_title() . '</h2>';
            echo get_the_content();
        }
        echo '</section>';
    } else {
        // no posts found
    }
    wp_reset_postdata();
?>
0

I also discovered this answer, which I prefer, because I'm able to use the standard template tags to call up information:

<?php
global $post;
$args = array(
        'post_parent'            => '46',
        'post_type'              => 'page',
        'order'                  => 'ASC'
    );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <section class="page-content">
        <h2><?php the_title(); ?></h2>
        <?php the_content(); ?>
    </section>
<?php endforeach; 
wp_reset_postdata();?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.