Posted on

Adding a post to a page

The code below is a complete working model that will allow you to add a post to a page and filter that post based on the category it is in.

For example, you might want to list only news items on your home page along with the standard page content. This code will allow you to do that.

There is very little formatting in this code so you will need to add your own style to it once its on your site.You can copy and paste this code exactly as it is to your site and it will work right away.

<!– Start Box Array 1 –>

<?php
$cat_id = 1; //whatever category id you need
$args=array(
‘post_type’ => $type,
‘post_status’ => ‘publish’,
‘posts_per_page’ => 1,
‘caller_get_posts’=> 1,
‘category__in’ => array($cat_id)
);
$my_query = new WP_Query($args);
if ( $my_query->have_posts() )
while ( $my_query->have_posts() ) :
$my_query->the_post(); ?>

<?php
global $more;
$more = 0;?>
<h2><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></h2>
<p><?php the_content(‘Continue Reading’); ?></p>

<?php endwhile;
wp_reset_query(); // Restore $post
?>
<!– End Box Array 1 –>

To make changes to the code that will work with your category is very simple.

To change the category id you need to first get your category number and change the RED text on the following line.

$cat_id = 1; //whatever category id you need

If you want to display more then one post for your selected category then you will need to change the following number to the amount of posts you want to display.

‘posts_per_page’ => 1,

Formatting how your post looks is carried out on the following code.

<h2><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></h2>

<p><?php the_content(‘Continue Reading’); ?></p>

NOTE: You can use the full code above as many times as you like on your page. By changing just the category ID in each instance the content and layouts that you can setup are endless.