Hello,
Trying to put my wordpress hat back on after a few month’s break.
I have a site that consists of pages, but there is also a page of posts.
Anyway, on the footer of all pages and posts I would like to show an excerpt of the latest post and a thumbnail image from this post.
I’m having trouble finding a solution! If I use the get_excerpt, it displays the excerpt for the page I’m on, not the latest post. Also not sure if I can have two loops on one page?
Any ideas? I would like to put the solution within the footer.php file.
Thanks
Ally
OK I’ve figured this one out, so here’s how I did it in case anyone’s interested.
First in my footer.php I put the following code:
<!--code for latest post excerpt-->
<?php
$args = array( 'numberposts' => 1, 'order'=> 'DESC', 'orderby' => 'post_date' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div>
<p><?php getImage('1'); ?></p>
<h4><a href="<?php the_permalink(); ?>" rel="bookmark" title="View and comment on <?php the_title(); ?>"><?php the_title(); ?></a></h4>
<p><?php the_content(); ?></p>
</div>
<?php endforeach; ?>
The ‘numberposts’ argument is set to 1 because I only want to show the latest post. Order is DESC so most recent is first, based on orderby being post date.
I added p and heading tags to make it look right.
The getImage refers to a handy piece of code I found here: http://bavotasan.com/tutorials/retrieve-the-first-image-from-a-wordpress-post/
As well as the getImage call above you also need to put some code in functions.php, see the link for more info.
I changed Bavotasan’s code slightly, so the preg_replace line is as follows:
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '/width="120px" height=""/',
I added a replacement to the argument so the image size is changed to 120px wide, which is how I want it to be in the footer (otherwise his code just uses the image size from the post).
All good!