Reputation: 6996
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.
Upvotes: 0
Views: 133
Reputation: 11
try this code,
<?php
get_a_post(Post Id);
the_content();
?>
As per this question: 1 - Is this fine? I spoke to some people who "know" WP, and they said I should avoid getting posts into my html. my answer is: if you call any specific post in html which is obviously admin manageable, there are lots of chances that same post can get deleted by mistake then the above code will not work as the post id could not be retrieved so we basically make a category and add the post in it. For example:
<?php
$args = array('category' => cat id, 'numberposts' =>1);
$postslist = get_posts($args);
foreach ($postslist as $post) : setup_postdata($post);
the_title();
the_content();
endforeach; ?>
Upvotes: 1
Reputation: 47189
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:
<?php
$my_id = 7;
$post_id_7 = get_post($my_id);
$content = $post_id_7->post_content;
?>
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.
Upvotes: 0