I just did some upgrades on my Sulumits Retsambew theme. I added latest blog post to the static frontpage. I wanted to have combination of static page with dynamic content. So my fixed text is always above with the latest blog post from some category. Since I lost an hour with that I thought maybe you would have use of it.
Here is how to do it:
First thing if you use static page is to make template for that specific page. So first make the template let say frontpage.php. On the beginning of the code add this:
<?php
/*
Template Name: Front page
*/
?>
Then just copy all content from page.php bellow above code in frontpage.php.
Now we have customized code for our page. Go to Wordpress -> edit pages, on assign this template to our given page. Usually frontpage page.
Now we well add Latest post to our frontpage.
This code
<?php query_posts('showposts=1&cat=23'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="storycontent">
<?php the_excerpt(); ?>
<div class="date">
<div class="meta">
<?php _e("Posted in "); ?> <?php the_category(',') ?> — <?php the_author() ?> @ <?php the_time() ?> @ <?php the_date('','',''); ?> <?php edit_post_link(__('Edit This')); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
will show last post title, link, and default number of words from last post. Put it on needed position in your frontpage.php. Make wanted tweaks in css for post, storycontent, date, meta or just replace this with equilalents from your template. Also you can wrap all this in another div and gave the latest post special look.
Now we want to change the amount of text that the excerpt() function return us!
In your theme function.php file add this:
add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($length) {
return 150; }
This code will modify excerpt and instead of default 100 it will show 150 words from given post.
You will notice that excerpt function has missing „Read more“ link at the end, and instead it will show [...].
You will replace [...] with the „Read more“ link with this code:
function excerpt_ellipse($text) {
return str_replace('[...]', ' <a href="'.get_permalink().'">Read more...</a>', $text); }
add_filter('the_excerpt', 'excerpt_ellipse');
Add it also into your theme functions.php.
Be careful, to not add any space to the beginning or the end of the functions.php or you will get the „Cannot modify header information – headers already sent function.php wordpress“ error.
That is it. To see how this work check my Sulumits Retsambew frontpage. Notice that I show posts only from category that is related to the Sulumits Retsambew.