Display Particular Post/Page in WordPress

WordPress is now widely used in different types of websites. It’s perfect for blogging and a good CMS for company and personal websites.

As a web developer, we should maximize this feature of WordPress. One particular scenario is when the client needs to show an excerpt of a particular article in a page.

A bigger problem will come if he wants you to show 10 articles from different categories. Using a plugin is not advisable at this time. It’s time to face you’re best text editor and start coding.

The key code here is

<?php query_posts( 'page_id=[page ID]' );?>

and

<?php query_posts( 'p=[post ID]' );?>

Display Particular Post outside the Loop

If the article that you need to show in a specific page is a post you can use the code below:

<?php query_posts( 'p=102' );?>
	<?php while ( have_posts() ) : the_post(); ?>
		<?php /*?>put what you need here<?php */?>
	<?php endwhile; ?>
<?php wp_reset_query(); ?>

In the code above, the ID of the post that we retrieve is 102.

Display Particular Page outside the Loop

If you need to show a page in a particular space on the web page, use the code below:

<?php query_posts( 'page_id=437' );?>
	<?php while ( have_posts() ) : the_post(); ?>
		<?php /*?>put what you need here<?php */?>
	<?php endwhile; ?>
<?php wp_reset_query(); ?>

The page with 437 ID will show.

As you can see, we use

<?php wp_reset_query(); ?>

This is to avoid conflicts on queries. It’s a good practice to always reset the query to avoid confusion on other posts or pages.

This is a very important code if you’re retrieving more than one particular article in a single page.

Complete Code Example

<?php get_header(); ?>

<div class="fav-articles">
	<div class="fav page">
		<?php query_posts( 'page_id=437' );?>
			<?php while ( have_posts() ) : the_post(); ?>
				<h3><?php the_title(); ?></h3>
				<?php the_excerpt(); ?>
			<?php endwhile; ?>
		<?php wp_reset_query(); ?>
	</div>

	<div class="fav page">
		<?php query_posts( 'page_id=440' );?>
			<?php while ( have_posts() ) : the_post(); ?>
				<h3><?php the_title(); ?></h3>
				<?php the_excerpt(); ?>
			<?php endwhile; ?>
		<?php wp_reset_query(); ?>
	</div>

	<div class="fav article">
		<?php query_posts( 'p=445' );?>
			<?php while ( have_posts() ) : the_post(); ?>
				<h3><?php the_title(); ?></h3>
				<?php the_excerpt(); ?>
			<?php endwhile; ?>
		<?php wp_reset_query(); ?>
	</div>
</div>

<?php get_footer(); ?>

Where to Find the ID of the Post/Page?

The ID will show above the windows task bar when you hover the title of the article. Works on Mozilla and Chrome

You can see the ID in the URL when you edit the page