A newbie at wordpress, trying to figure how to retrieve a specific post content into a div.
This is what I have as of now,
<div style="margin-top: 100px;">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
</div>
while this works great, I have some questions,
1 – Is this fine? I spoke to some people who “know” WP, and they said I should avoid getting posts into my html.
2 – If I replace the above code with,
<div style="margin-top: 100px;">
<?php $postId = 1; get_post($postId) ?>
<?php the_content(); ?>
</div>
It doesn’t work.
3 – What modifications do I need to make to this code to make it work?
REQUEST: please don’t move this to wordpress.stackexchange.com, as there is minimal activity there.
cheers.
Take a look at the Function Reference for get_post
http://codex.wordpress.org/Function_Reference/get_post
The examples are illustrative. To get the content of the post, you would do something like this:
Note that we have used get_post to retrieve the database record we are interested in, and from among the fields returned, we are making use of post_content.
This is different from using the_content, which displays the content of the post currently being processed by The Loop.
http://codex.wordpress.org/Function_Reference/the_content
I imagine the former approach is better for what you are trying to do.