Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to get the content from another outside the loop? The ID is 302 and I need to display the content of that on another page.

share|improve this question
add comment

4 Answers 4

up vote 15 down vote accepted

You can use get_page() to return the $post object of a static page:

$page_id = 302;
$page_object = get_page( $page_id );
echo $page_object->post_content;

Edit

Similarly, you can use get_post() to return the $post object of a post:

$post_id = 302;
$post_object = get_post( $post_id );
echo $post_object->post_content;
share|improve this answer
    
Do shortcodes still work when using this? –  Tim Feb 20 at 21:35
    
Just found out it doesn't. Use setup_postdata( $post ); first, after that, you can use the_content(); –  Tim Feb 20 at 21:39
add comment

For completeness, building on Tim's comment above and inspired by Stephen Harris's article, the solution that enables use of the_content() is:

$post_id = 302;
global $post;
$post = &get_post($post_id);
setup_postdata( $post );
the_content();
wp_reset_postdata( $post );

And hence filters get applied (paragraphs will be inserted etc.) and shortcodes work.

share|improve this answer
add comment

you can put content in a category X and use query_post before while like this :

    <?php query_posts('cat=X&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
          <?= get_the_content(); ?>
    <?php endwhile; ?>
share|improve this answer
add comment

If your content include shortcodes, you should use:

$post_id = 22;        
$post_object = get_post( $post_id );        
echo do_shortcode( $post_object->post_content );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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