views:

28

answers:

1

Hello, I'm creating magazine style theme (not e-commerce) and I want to display 3 latest posts from ex. X, Y and Z category, where this 1st post will be with thumb and other 2 only titles. I found some similar solution themes, but when I look into the code, they created 2 loop for each category (2x3=6) and with this 6 loops code looks very messy. So I decided to create function (ex. latest_post_from_category($cat);) to display this post.

Here comes the question is my decision right, if yes do have any advices to make this function more flexible?

Thanks for your time.

+1  A: 

a function can become more flexible with params and switches. example follows

function getPosts($type,$return = false,$amount = 4)
{
   switch($type)
   {
      case 'comments':
         //Get latest comments here
      break;
      case 'posts':
      case 'posts-desc':
      case 'posts-asc':
          if($type == 'posts-asc'){ $order = 'ASC';}else{$order = 'DESC';/*default*/}
          //Get posts
      break;
      /*(etc...etc)*/
   }
}

$comments = getPosts('comments',true,5); //5 comments

$posts= getPosts('posts-desc',true,6); //5 Latest

Things like that can really make a design come together.

The Thumbs

In regards to this you only really need the post id and wordpress provide the functions so with my example above you can loop and do an if statement

$i = 0;
foreach(getPosts('post-asc',true,3) as $row)
{
    $i++;
    if($i == 1)
    {
       //Show thumb for $row
       if(!wct_display_thumb("width:200px;height:150px", $row->ID))
         {
            //Show title
         }
    }else
    {
       //Show title for $row!
    }
}
RobertPitt
Thanks Robert for your answer, I know that WordPress provide function get_posts which takes post ID and returns database record for that posts. If it's not hard for you can you write about our methods more extensively. Thanks for you time again.
Mamaduka
Iot would be pointless for me to write it out when there already done: http://codex.wordpress.org/WordPress_Lessons
RobertPitt
Thanks Robert this information is very helpful.
Mamaduka
Robert I have a question about 'wct_display_thumb', is this wordpress's built in function?
Mamaduka
Sorry about not informing you but its a plugin, http://wordpress.org/extend/plugins/wp-choose-thumb/ there is also a wordpress version found at http://codex.wordpress.org/Function_Reference/wp_get_attachment_thumb_url
RobertPitt
Yes, found this plugin when searching info about 'wct_display_thumb', I think'll use wordpress version
Mamaduka
No problem, always look at the Wordpress Codex / Reference pages when in need of a function and its information.
RobertPitt
Also, Can you please select the answer as Accepted if you have accepted this answer.
RobertPitt