Show content from multiple pages (not posts) on home page

admin2025-06-07  48

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; ?>

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; ?>

Share Improve this question edited Oct 20, 2018 at 16:39 Nick J. asked Oct 20, 2018 at 16:22 Nick J.Nick J. 11 bronze badge 0
Add a comment  | 

3 Answers 3

Reset to default 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();

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();
?>

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();?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749238039a317517.html

最新回复(0)