Breaking

Tuesday, January 5, 2016

Define How Blog Posts Are Displayed On The Home Page

The problem. I’ve always wondered why 95% of bloggers displays all of their posts the same way on their home page. Sure, WordPress has no built-in option to let you define how a post is displayed. But wait: with custom fields, we can do it easily.
The solution. The following hack lets you define how a post is displayed on your home page. Two values are possible: Full post
Post excerpt only
Once more, we’ll use a custom WordPress loop. Find the loop in your index.php file and replace it with the following code: 


view plaincopy to clipboardprint?
 
<?php if (have_posts()) :
while (have_posts()) : the_post();

$customField = get_post_custom_values("full"); if (isset($customField[0])) {
//Custom field is set, display a full post the_title();
the_content();

} else {
// No custom field set, let's display an excerpt the_title();
the_excerpt();

endwhile; endif;
?>

In this code, excerpts are displayed by default. To show full posts on your home page, simply edit the post and create a custom field called full and give it any value.
Code explanation. This code is rather simple. The first thing it does is look for a custom field called full. If this custom field is set, full posts are displayed. Otherwise, only excerpts are shown.


No comments:

Post a Comment