Digital Brent
Digital Brent

Reputation: 1285

How to identify post id in wordpress

I am designing a homepage for my WordPress theme. I'm trying to figure out how to take content from various pages (the content entered into a page in the admin panel) and display it on the homepage. I'd like the homepage to be a sort of sampler for the rest of the site. You can see what I have so far if you go to my website. I'd like each of those little panels of text to display content from a different page.

I think I'm supposed to use get_post(), but I can't figure out how to identify the post id or tell it to look for posts or content from different pages. Here is the function I have written so far:

<?php get_post(5); ?>

Where I have switched out the number 5 for other numbers but still can't get it to work.

Can anyone tell me if I'm on the right track or is there a different function for getting content from different pages? If no, then how do I know what the post ID is?

Any advice is greatly appreciated. Thank you.

Upvotes: 0

Views: 223

Answers (2)

nitrammit
nitrammit

Reputation: 143

This is what I use when I need to pull content from a specific page. It fetches the content from the page in question, shows the content in a standard WP loop, and then resets the loop to normal at the end.

<?php query_posts('page_id=5'); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php the_title(); ?>

<?php the_content(); ?>

<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

Obviously change the page_id=5 to match the ID of your page.

Upvotes: 1

Ivan Han&#225;k
Ivan Han&#225;k

Reputation: 2306

As far as I am concerned, you are a halfway from a good style.

It depends, what are you trying to do.

The easiest way of getting your posts is through Loop.

You get everything, ID, title, content… for a couple of posts, you are gonna decide, what posts (from any category, order…)

btw: can do it with a bazillion of WP functions, query_posts, get_pages, get_posts, WP_Query… varying in arguments style

Function you used, get_post is designed to grab data (id, title, content) for only one particular post, or if you have your IDs defined beforehand, you use it in loop. However, if you want to get more than one post, rather use functions mentioned above (I think the most important thing is to grab them in one DB query and not in ineffective way one-by-one.

Upvotes: 0

Related Questions