0

I want to include the content of an existing page in the header.php file? What's the easiest way to do this? I am guessing that there is a way to load an existing page via a php call, however I am not sure of the correct syntax.

Thanks, in advance.

1 Answer 1

1

You could instantiate a new query:

$query = new WP_Query( 'page_id=7' );

After doing so, you make a loop to display the query's content.

<?php if ( $query->have_posts() ) : ?>
    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <?php wp_reset_postdata(); // Important, so this loop does not affect the global post object afterwards ?>
<?php endif; ?>

This is based on WP's official codex: https://codex.wordpress.org/Class_Reference/WP_Query

Sign up to request clarification or add additional context in comments.

4 Comments

Notice that the query targets a single page ID
Thanks for the help. So this seems to include only the title of the page and not the content - I would like the content as well? Also, I think in the while loop, "$the_query" is supposed to be "$query"? Thanks!
So adding, "the_content()" in place of "the_title()" seems to solve my issue. Thanks!
You are totally right. My mistake. And yes, once in the loop, you may add any template tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.