WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
    
Don't know the ID? Make use of get_queried_object_id() ! developer.wordpress.org/reference/classes/wp_query/… – jave.web Apr 12 at 16:13
up vote 29 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 Baas Feb 20 '14 at 21:35
    
Just found out it doesn't. Use setup_postdata( $post ); first, after that, you can use the_content(); – Tim Baas Feb 20 '14 at 21:39

to get the content of the post outside the loop you can write something like this

global $post;
$content = $post->post_content;

if ( !empty( $content ) ) :
    echo $content;
endif;
share|improve this answer

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
    
Don't do shortcode kovshenin.com/2013/dont-do_shortcode – Brad Dalton Jun 24 '15 at 21:52

Since you know your target post ID (302), you may find useful this shorthand syntax that you can use out of the loop (though its performance is pretty much the same as in any other alternative method:)

echo(get_post_field('post_content',302));
share|improve this answer

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
    
This almost crashed my browser :/ taking out the ampersand helps, but it's still not parsing shortcodes. – Zade Oct 5 '15 at 8:12

You can use the get_post_data() function to get post outside the loop. Place this code in functions.php

function get_post_data($postId) {
    global $wpdb;
    return $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID=$postId");
}

and then add this snippet for more control on the process

<?php $data = get_post_data(302);
    echo $data->post_date;     // post date
    echo $data->post_title;    // post title
    echo $data->post_content;  // post content
    echo $data->comment_count; // comments number
?>
share|improve this answer

You can simply call get_the_content(postId)

<?php echo get_the_content($postId); ?>
share|improve this answer

use wp_reset_query(); it will work..

<?php query_posts( 'post_type=posttype&p=302' ); 
    if( have_posts() ) : while( have_posts() ) : the_post(); ?>
        <?php the_content(); ?>
    <?php endwhile; endif; 
wp_reset_query();
?>

posttype can be "post" , "page" or your custom post type. Here p=302 is your post id.. Hope it will work.

share|improve this answer
2  
Never use query_posts unless you need to break page functionalities. Always use WP_Query or get_posts for custom queries :-) – Pieter Goosen Apr 4 '15 at 6:55
    
yes.. you are right.. It can Wp_Query as well.. same result found.. – Jahirul Islam Mamun Apr 5 '15 at 19:23

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

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.