0

Im trying to create an array for my bespoke homepage template which gets the title, category, featured thumbnail and excerpt from the last 3 blog posts and puts them into the correct divs for styling.

So far i have managed to come up with the following:

<div id="content2Wrapper">

 <?php
$args = array( 'numberposts' => 3, 'order'=> 'DSC', 'orderby' => 'date' );
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post); ?> 

<div>
    <div id="widget2left">
        <h3><?php echo substr(get_the_title(),0,58); ?></h3><p>
        <h5><?php echo substr(the_category(),0,70); ?></h5>
        <p><?php echo substr(get_the_excerpt(),0,165); ?><p> 
        <br> <p><?php the_post_thumbnail(220,120,true); ?> </p>
    </div><!--end widget 2 left-->

</div>
</div><!--end wrapper-->

But as you can see this displays 3 posts order by date, but the substr code is cutting the ends of words off & also the images are not displaying at the 220*120 resolution.

here is an image of what i am trying to acheive http://s16.postimg.org/eqqsent91/screen2.png

My code knowledge is rusty so any help to improve this code would be really helpful.

2
 <div id="content2Wrapper">

 <?php
$args = array( 'numberposts' => 3, 'order'=> 'DSC', 'orderby' => 'date' );
$postslist = get_posts( $args );
while(have_posts()) :  the_post(); ?> 

<div>
    <div id="widget2left">
        <h3><?php the_title(); ?></h3><p>
        <h5><?php the_category(','); ?></h5>
        <p><?php the_excerpt(); ?><p> 
        <br><p><?php the_post_thumbnail(array(220,120)); ?> </p>
    </div><!--end widget 2 left-->

</div><?php endwhile; ?>

I suggest you to not to use substr with title and category function and to trim post excerpt you can set limit in you functions.php file ( example ) and for displaying image in proper size you can also use add_image_size function to define a new image size in your functions.php file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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