Here is the website http://ariadneswonderland.gr/ that all this concerns.

On the homepage I'm using a Drop shadow box plugin under the gallery to show a series of images of featured posts, as of right now, as i'm still building the website all you see is the White box under the gallery and a emoticon inside it..

What i'd like is to be able to do is have some code that pulls the 5 lastest posts from a category (in this case frontpage id 7) and Displays the Featured Post Image next to each other.



= X X X X X =

Where X is a Featured post image automatically retrieved instead of having to retrieve and add links every time i want to change a feature. = is the automatically generated [dropshadowbox].

Any ideas/code/snipets/plugins that you might feel would work would be greatly appreciated.

So far however all i've come up with is this but I can't figure out how to implement the php into the post, phpexec and similar plugins don't seem to work for this, tho simple things like echo test; work fine. And making it a function doesn't work as it doesn't seem to return anything :S Obviously I need alot more coding experience but as of right now I'd like to just be done with this website.

PHP Code:
// fetching latest posts from specific category 
$categoryId = 7; 
$args = array('category' =>  categoryId  , 'post_status' => 'publish', 'numberposts' => 5); 
$posts = get_posts( $args ); 
foreach($posts as $post){ 
$feat_image_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); 
$post_url = get_permalink($post->ID); 

2 Answers 2

<?php /* Set the name of the category and the number os posts to be displayed */?>
<?php $first_query = new WP_Query('category_name=name&posts_per_page=7'); ?>
<?php /* Show the posts */ ?>
<?php while ($first_query->have_posts()) : $first_query->the_post(); ?>
    <?php  /* Make the hiperlink to the post */ ?>
    <a href="<?php the_permalink(); ?>">
        <?php /* Get the featured post image */ ?>
        <div class="post-image"> <?php /* This div is just for help you to organize your posts images */ ?>
            <?php the_post_thumbnail(); ?>
<?php endwhile; // End the loop. Whew. ?>

In the second line, chose the name of the category, and the number of posts that will be displayed in this page. The function get the featured post image and insert into your webpage.


i suggest you use that code in the .php file itself. if its a page with a unique template, change that template file.

if not, edit the page.php ( or single.php ) and add a conditional for the home page id and include your code.

i didn't test your code but if its correct something like this would work:

if ( get_the_ID() == '99' ) {
   // your code
  • The problem lies in that the code doesn't work :S I like the conditional idea, but it'd be easier to make a home.php template tbh and might make more sence. I tried adding a count=1 i= etc to have it loop 5 times and then echo the html for each image, but that somehow made the bug way worse...
    – presdec
    Commented Jul 27, 2012 at 20:35
  • yes, having a template is the best way :) why not just use the_post_thumbnail(); inside the foreach loop? or is the loop itself that's not showing your posts?
    – pcarvalho
    Commented Jul 27, 2012 at 20:55
  • Thanks for the help. After struggling i managed to get it working. $categoryId = 7; $args = array('category' => $categoryId , 'post_status' => 'publish', 'numberposts' => 5); $posters = get_posts( $args ); if ( $posters ) { foreach ($posters as $post) { $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full"); ?> <a href="<?php echo get_permalink($post->ID); ?>" title=""><img src="<?php echo $imgsrc[0]; ?>" class="thumbnail" width="130" height="130" alt='' /></a> <?php } } I've made it into a shortcode [featuredposts category=7].
    – presdec
    Commented Jul 27, 2012 at 23:36
  • It looks a mess in here but apparently i can't answer my own question in under 8 hours :P Feel free to re-post the solution if u want to.
    – presdec
    Commented Jul 27, 2012 at 23:40
  • i've updated your question as it was our own solution, but feel free to mark my answer if it helped ;)
    – pcarvalho
    Commented Jul 28, 2012 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.