Post Pic

WordPress hack: Embed a page into another page

Are you using different page templates on your WordPress theme? If yes, here is a cool hack that allow you to embed a page into another page.

Paste the code below within the loop. Make sure to update page ID on line 1!

<?php $recent = new WP_Query("page_id=**ID**"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile; ?>

Thanks to CSS Tricks for the tip!

4 Responses

Feb 23 2012 18:05

Usefull,
But do not forget to call the function wp_reset_postdata(); after the endwhile so the main query will not be overridden :) .

Rahe

Feb 24 2012 07:07

I’ve used an alternative solution in the past with success:

http://stackoverflow.com/questions/4090698/wordpress-include-content-of-one-page-in-another

Just throwing it out there as another option.

Feb 25 2012 09:24

This trick is seems to be easy but can you tell me what is the advantage of embedding one WordPress page to another? I have done it with my one WordPress page.

And when I put the wp_reset_postdata(); it gives error at line 40 .

Mar 31 2012 13:32

Hi @Tushar try wp_reset_db() or $wpdb->flush().

hope that helps.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox